-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compiler warning unused_mut suggests removing mut keyword, but it would break compilation #113451
Comments
Slightly smaller https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e1265fe583e7043654de444f760af008: #![allow(unused_must_use)]
enum State {
Waiting { start_at: u64 }
}
fn main() {
let current_time = 0u64;
match (&mut State::Waiting { start_at: 0u64 }) {
State::Waiting { mut start_at } => {
current_time >= start_at;
}
}
} (Note that this snippet contains another problem, incorrectly fired |
A better way to do this is proposed in rust-lang/rfcs#3410. |
I just had a similar issue with false-positive It happened on the latest stable:
|
My colleague has found a workaround for our issue: making the parameter itself immutable and then rebinding it to a mutable local variable. For that variable, the false warning is no longer generated. Sorry for still not making the reproducer |
Ok, sorry, now I'm pretty sure that my issue is caused by a proc macro and it's not a compiler bug. The linked issue above contains a reproducible example |
Minimal example from rust-lang/rust-clippy#12561 (comment): fn main() {
let (mut a,) = &mut (0,);
let _: i32 = a;
} |
The Rust compiler gives a warning the
mut
keyword on a pattern is not needed, and suggests removing it. However, removingmut
makes the code invalid and it then fails to compile.I tried this code:
(Rust Playground Link)
I expected to see this happen: if the code compiles successfully, a warning and its suggested fix should not break the code so it then fails to compile.
Instead, this happened:
And if we remove
mut
from{ mut start_at }
in the code, the compilation fails:Or, we could use the
cargo fix
feature, and it results in the same issue:Goal of the sample code
The code that was used (and generates an
unused_mut
warning) was trying to match on aCopy
type (in this caseu64
). We want to use it by value, and adding themut
keyword to the pattern was used in order to bind by value instead of by reference.There may be a better way to implement the sample code -- that is perhaps a related issue. It is true that the
mut
is unneeded. The bound variable is only used to read the value. But it must be matched by value and not by reference, and addingmut
is the only way I know to do that.(There is the alternative of matching by shared reference and adding an immediately following statement
let start_at = *start_at;
to rebind it by value (it isCopy
), but that is also an ugly hack.)Meta
Occurs on both 1.70 stable and 1.72 nightly:
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: