Skip to content

Inhibit unused_mut variables if unused_variables is also triggered #97074

Open
@djc

Description

@djc

Given the following code:

fn main() {
    let mut s = String::from("a");
}

(Playground.)

The current output is:

warning: unused variable: `s`
 [--> src/main.rs:2:13
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
2 |     let mut s = String::from("a");
  |             ^ help: if this is intentional, prefix it with an underscore: `_s`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: variable does not need to be mutable
 [--> src/main.rs:2:9
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
2 |     let mut s = String::from("a");
  |         ----^
  |         |
  |         help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` on by default

warning: `playground` (bin "playground") generated 2 warnings
warning: unused variable: `s`
 [--> src/main.rs:2:13
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#)  |
2 |     let mut s = String::from("a");
  |             ^ help: if this is intentional, prefix it with an underscore: `_s`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: variable does not need to be mutable

It's obvious that an unused variable does not need to be declared mutable, and the extra warning puts more cognitive burden on the reader to figure out what's relevant.

Found this on 1.60.0, but also still happens on nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions