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

False negative for unused_mut when writing to field through reference #110849

Closed
lukas-code opened this issue Apr 26, 2023 · 0 comments · Fixed by #110960
Closed

False negative for unused_mut when writing to field through reference #110849

lukas-code opened this issue Apr 26, 2023 · 0 comments · Fixed by #110960
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@lukas-code
Copy link
Member

Code

I tried this code:

pub struct Foo(i32);

pub fn foo(mut x: &mut Foo) {
    x.0 = 1;
}

I expected to see this: The compiler should tell me to remove the mut, since it isn't necessary:

warning: variable does not need to be mutable
 --> src/lib.rs:3:12
  |
3 | pub fn foo(mut x: &mut Foo) {
  |            ----^
  |            |
  |            help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` on by default

Instead this happened: The code compiles without a lint.

The lint triggers correctly with an explicit reborrow: (&mut *x).0 = 1;

Meta

playground 1.71.0-nightly (2023-04-25 458d4da)

@rustbot label A-diagnostics A-lint

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. labels Apr 26, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 28, 2023
…errors

fix false negative for `unused_mut`

fixes rust-lang#110849

We want to avoid double diagnostics for code like this, but only if an error actually occurs:
```rust
fn main() {
    let mut x: (i32, i32);
    x.0 = 1;
}
```

The first commit fixes the lint and the second one removes all the unused `mut`s it found.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 28, 2023
…errors

fix false negative for `unused_mut`

fixes rust-lang#110849

We want to avoid double diagnostics for code like this, but only if an error actually occurs:
```rust
fn main() {
    let mut x: (i32, i32);
    x.0 = 1;
}
```

The first commit fixes the lint and the second one removes all the unused `mut`s it found.
RalfJung pushed a commit to RalfJung/miri that referenced this issue Apr 30, 2023
fix false negative for `unused_mut`

fixes rust-lang/rust#110849

We want to avoid double diagnostics for code like this, but only if an error actually occurs:
```rust
fn main() {
    let mut x: (i32, i32);
    x.0 = 1;
}
```

The first commit fixes the lint and the second one removes all the unused `mut`s it found.
kaivol added a commit to kaivol/windows-rs that referenced this issue May 3, 2023
kaivol added a commit to kaivol/windows-rs that referenced this issue May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants