rust icon indicating copy to clipboard operation
rust copied to clipboard

"unused_mut" is not detected when variable type is mutable reference and the referent is mutated

Open QuestionDriven opened this issue 4 years ago • 3 comments

Code:

fn main() {
        let mut point = &mut Point { x: 10, y: 20};
        point.x = 30; 
        println!("{:?}", point);
}

#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

(Playground)

Current behavior:

no warning

Expected behavior:

warning: variable does not need to be mutable
 --> src/main.rs:2:13
  |
2 |         let mut point = &mut Point { x: 10, y: 20};
  |             ----^^^^^
  |             |
  |             help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` on by default

QuestionDriven avatar Aug 13 '21 12:08 QuestionDriven

I think we should search systematically for all similar diagnostic holes/bugs regarding unnecessary muts. I filed two similar things for Clippy, but I think such mut diagnostics should be in rustc.

leonardo-m avatar Aug 13 '21 12:08 leonardo-m

If this lint had existed, it would have helped me learn the rules around mut <ident> and &mut reborrowing faster.

kpreid avatar Aug 17 '21 22:08 kpreid

This was fixed by https://github.com/rust-lang/rust/pull/110960 and the fix is included in Rust 1.71.0.

lukas-code avatar Jul 13 '23 21:07 lukas-code