Skip to content

"variable does not need to be mutable" not detected #61424

@ghost

Description

(sorry if this is an duplicate or the wrong repo 😇 )

fn main() {
    let mut name;
    name = format!("foo{}", "bar");
    dbg!(name);
}

(Playground)

Current behavior:
no warning

Expected behavior:

warning: variable does not need to be mutable
 --> src/main.rs:2:9
  |
2 |      let mut name;
  |          ----^
  |          |
  |          help: remove this `mut`
  |
  = note: #[warn(unused_mut)] on by default

The variable clearly doesn't need to be mutable, still, nothing is detected and no warning is emitted.

Rust 1.35.0
(Using the playground, also in 1.37.0-nightly (2019-05-31 7840a0b753a065a41999))

Activity

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
C-bugCategory: This is a bug.
on Jun 1, 2019
czipperz

czipperz commented on Jun 1, 2019

@czipperz
Contributor

The warning appears again if you switch to 2015 edition. Huh. Nice catch!

czipperz

czipperz commented on Jun 1, 2019

@czipperz
Contributor

I found that Stable 2015 edition produces the warning and everything else doesn't.

| Warning | Optimization | Channel | Edition |
|---------+--------------+---------+---------|
| Present | Debug        | Stable  |    2015 |
| Bug!    | Debug        | Beta    |    2015 |
| Bug!    | Debug        | Nightly |    2015 |
| Present | Release      | Stable  |    2015 |
| Bug!    | Release      | Beta    |    2015 |
| Bug!    | Release      | Nightly |    2015 |
| Bug!    | Debug        | Stable  |    2018 |
| Bug!    | Debug        | Beta    |    2018 |
| Bug!    | Debug        | Nightly |    2018 |
| Bug!    | Release      | Stable  |    2018 |
| Bug!    | Release      | Beta    |    2018 |
| Bug!    | Release      | Nightly |    2018 |
added
A-NLLArea: Non-lexical lifetimes (NLL)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jun 1, 2019
jonas-schievink

jonas-schievink commented on Jun 1, 2019

@jonas-schievink
Contributor

cc @matthewjasper (likely caused by NLL migrate mode, which was enabled in #59114 for the 2015 edition)

matthewjasper

matthewjasper commented on Jun 1, 2019

@matthewjasper
Contributor

Yes, looks like another nll bug. If someone wants to have a go at this, the issue is that we are treating DropAndReplace differently to Assign for the purposes of this lint.

self-assigned this
on Jun 1, 2019
czipperz

czipperz commented on Jun 1, 2019

@czipperz
Contributor

I can try doing it I just don't know here to begin.

jonas-schievink

jonas-schievink commented on Jun 1, 2019

@jonas-schievink
Contributor

@czipperz I don't know this code too well, but take a look at src/librustc_mir/borrow_check/used_muts.rs. The first thing I'd try is adding a case for DropAndReplace to the match in visit_statement that mirrors what's done for Assign.

czipperz

czipperz commented on Jun 1, 2019

@czipperz
Contributor

Makes sense thank you. I was looking at librustc_lint and couldn't find anything.

ghost changed the title [-]"variable does not need to be mutable" no detected[/-] [+]"variable does not need to be mutable" not detected[/+] on Jun 2, 2019
added 3 commits that reference this issue on Jun 4, 2019
0f732bf
ae4939e
f8a2363
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-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

    Participants

    @jonas-schievink@varkor@czipperz@matthewjasper

    Issue actions

      "variable does not need to be mutable" not detected · Issue #61424 · rust-lang/rust