rust-clippy
rust-clippy copied to clipboard
Lint needless `&mut` when `&` suffices
Idea: Add a lint for unneeded mutable borrows when an immutable borrow suffices:
fn main() {
let m = &mut 21; // `&21` would suffice
println!("{}", m);
}
This lint could also catch an unneeded ref mut when a plain ref suffices:
fn main() {
let test = &mut Test{a: 1};
let &mut Test{ ref mut a } = test;
println!("{}", a);
}
struct Test {
a: i32,
}
cc @oli-obk
Is this E-hard?
probably not, but it requires some gray matter wrangling. For this to be doable in a reasonable matter, we likely need to implement it on MIR and use dataflow to track mutable references.
Isn't this a duplicate of #353?