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