rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Lint needless `&mut` when `&` suffices

Open phil-opp opened this issue 9 years ago • 3 comments

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

phil-opp avatar Sep 16 '16 15:09 phil-opp

Is this E-hard?

pickfire avatar Oct 20 '20 16:10 pickfire

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.

oli-obk avatar Oct 21 '20 08:10 oli-obk

Isn't this a duplicate of #353?

GnomedDev avatar Oct 01 '24 21:10 GnomedDev