rust-lang / rust-lang/rust-clippy
Possible unit_arg false positive when passing `Default::default()` as function parameter resolving to unit value
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Lint name: unit_arg
I tried this code (Playground):
#[derive(Debug)]
struct Foo;
trait Factory: Sized {
type Options: Default;
fn create(options: Self::Options) -> Self;
}
impl Factory for Foo {
type Options = ();
fn create(_: Self::Options) -> Self {
Foo
}
}
fn main() {
let x = Foo::create(Default::default());
println!("{:?}", x);
}
In my real use case, the same pattern is employed for generalizing the passing of default options to a creator function, irrespective of implementation.
I expected this not to trigger unit_arg, and most importantly, not to recommend moving Default::default(); to the front while leaving a literal unit value in its stead, which could be incompatible with other implementations of Factory:
error: passing a unit value to a function
--> src/main.rs:21:13
|
21 | let x = Foo::create(Default::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> src/main.rs:1:9
|
1 | #![deny(clippy::unit_arg)]
| ^^^^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
help: move the expression in front of the call and replace it with the unit literal `()`
|
21 | let x = {
22 | Default::default();
23 | Foo::create(())
24 | };
|
Meta
cargo clippy -V: clippy 0.1.52 (9bc8c42 2021-05-09), also occurs in 0.1.54 (2021-06-09 eab201d)rustc -Vv:rustc 1.52.1 (9bc8c42bb 2021-05-09) binary: rustc commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53 commit-date: 2021-05-09 host: x86_64-unknown-linux-gnu release: 1.52.1 LLVM version: 12.0.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the reported Rust example with the unit_arg lint enabled and inspect the lint's implementation and existing tests. Confirm that Default::default() is resolved as a unit value only for this implementation, then add coverage and ensure the lint no longer recommends replacing a potentially generic default argument with ().
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100