Skip to content
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

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

Open
QuestionDriven opened this issue Aug 13, 2021 · 3 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unused_mut Lint: unused_mut T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@QuestionDriven
Copy link

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
@jonas-schievink jonas-schievink added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels Aug 13, 2021
@leonardo-m
Copy link

leonardo-m commented Aug 13, 2021

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.

@kpreid
Copy link
Contributor

kpreid commented Aug 17, 2021

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

@lukas-code
Copy link
Member

This was fixed by #110960 and the fix is included in Rust 1.71.0.

@jieyouxu jieyouxu added L-unused_mut Lint: unused_mut T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unused_mut Lint: unused_mut T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants