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 · 3 comments

Comments

@ehuss
Copy link
Contributor

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.)

@ehuss

This comment has been minimized.

Copy link
Contributor Author

commented May 8, 2019

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

@taiki-e

This comment has been minimized.

Copy link
Contributor

commented May 8, 2019

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

@mati865

This comment has been minimized.

Copy link
Contributor

commented Aug 9, 2019

@rustbot modify labels: +A-NLL

@rustbot rustbot added the A-NLL label Aug 9, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.