Skip to content

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

Closed
@lukas-code

Description

@lukas-code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions