Clippy removes variable bindings even if it's later being used.
Summary
This code:
fn returns_unit() {
()
}
fn returns_result(res: ()) -> Result<(), ()> {
Ok(res)
}
fn main() {
let res = returns_unit();
returns_result(res).unwrap();
}
will produce this error with cargo clippy --fix
after fixes were automatically applied the compiler reported errors within these files:
* src/main.rs
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag
The following errors were reported:
error[E0425]: cannot find value `res` in this scope
--> src/main.rs:11:20
|
11 | returns_result(res).unwrap();
| ^^^ not found in this scope
error: aborting due to previous error
Reproducer
No response
Version
rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: aarch64-apple-darwin
release: 1.75.0
LLVM version: 17.0.6
Additional Labels
No response
@rustbot label I-suggestion-causes-error
Starting from 1.79, clippy will simply replace the removed variable with (). Thank you for opening the issue!
Starting from
1.79, clippy will simply replace the removed variable with(). Thank you for opening the issue!
No problem! I really like your proposed solution here: https://github.com/m-rph/rust-clippy/commit/d92d7902c173681f6e5347590e938712345f0f36 though. For the reason that the sole reason why i am binding and using the unit as a variable is so that if i ever return something else from the upstream function, the fact it's being used will break the compiler, rather than be a silent bug.
@y21 @Manishearth do you have any ideas how we could accommodate this usage?
I think we could add a suggestion to disable the lint.
@arifd the only option I can see given the change is disabling the lint at the let statement. Clippy then will not replace the usages of the variable with ().
If you want to make sure that a function continues to return the unit type, you can also write let () = function();. If the function's return type changes to something else, you will also get a compile error.
The unit_arg lint will also warn you if you pass any expression of type () other than the () expression itself as an argument to a function, so that fix is making the right suggestion and following the idea of other lints IMO.
You are right, but removing res is removing meta information about about the (). and unlinking it's use. Say i bind the unit type twice and use one of them somewhere and the other somewhere else. Clippy has provided no benefit by removing my carefully considered variable names and replacing all of their uses with ()
This should cover the usage, but I don't mind reverting to the previous impl 🤔
fn main() {
#[allow(clippy::let_unit_value)]
let res = returns_unit();
returns_result(res).unwrap();
}
For me, the current impl is useless, destructive (removing a variable name), and actually prohibits a defensive coding pattern and I will just allow the lint workspace-wide. I recognise the argument that it is consistent with unit_arg.
But after looking at unit_arg I think that is an even more rediculous lint, since you can't pass a unit into a fn unless the function was already expecting a unit!!
Maybe I'm missing something there and would love if someone could defend it, but unit_arg at WARN feels like overreach to me. Given the premise is false.
I'd say this is a rather niche pattern, niche enough that I'd say that the correct path forward is to selectively allow the lint in these cases.
This is not a defensive coding pattern that tends to be that necessary in my experience.
However there is a lint that I have heard other people wanting in the past: a version of must_use that is must_use by default on all functions and types with an opt-out. That sounds like it would be useful for you.
Okay. I very much appreciate the input from everyone and for giving me the respect to listen to my concerns, and even re-opening the issue.
I trust that the collective generally does the right thing (and better than i could do if i were benevolent dictator), so I'm happy to concede here.
Thanks again