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 warning gives bad suggestion in a macro #60643

Open
ehuss opened this issue May 8, 2019 · 5 comments
Open

unused_mut warning gives bad suggestion in a macro #60643

ehuss opened this issue May 8, 2019 · 5 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-NLL Area: Non Lexical Lifetimes (NLL) C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented May 8, 2019

The following gives a suggestion to remove the mut which will cause the code to fail to compile.

macro_rules! array {
    ($($element:expr),*) => {{
        let mut array = Vec::new();
        $(
            array.push($element);
        )*
        array
    }};
}

fn main() {
    let _x: Vec<i32> = array![];
    let _y: Vec<i32> = array![1,2,3];
}
warning: variable does not need to be mutable
  --> src/main.rs:3:13
   |
3  |         let mut array = Vec::new();
   |             ----^^^^^
   |             |
   |             help: remove this `mut`
...
12 |     let _x: Vec<i32> = array!{};
   |                        -------- in this macro invocation
   |
   = note: #[warn(unused_mut)] on by default

This causes cargo fix to fail.

(Sorry if this is a dupe, I went hunting but couldn't find any issues that were the same.)

@Centril Centril added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 8, 2019
@ehuss
Copy link
Contributor Author

ehuss commented May 8, 2019

Note: This requires NLL. Using 2015 edition before #59114 does not emit a warning.

@taiki-e
Copy link
Member

taiki-e commented May 8, 2019

You probably can avoid this to using variables with names starting with _ in macro.

@mati865
Copy link
Contributor

mati865 commented Aug 9, 2019

@rustbot modify labels: +A-NLL

@rustbot rustbot added the A-NLL Area: Non Lexical Lifetimes (NLL) label Aug 9, 2019
@steveklabnik
Copy link
Member

Triage: this still gives the warning, and still fails to compile if you apply the warning.

@estebank
Copy link
Contributor

Triage: no change.

@estebank estebank added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Mar 15, 2023
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. A-NLL Area: Non Lexical Lifetimes (NLL) C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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

7 participants