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

FP unused_mut: macro #105149

Open
matthiaskrgr opened this issue Dec 1, 2022 · 0 comments
Open

FP unused_mut: macro #105149

matthiaskrgr opened this issue Dec 1, 2022 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

Given the following code:

macro_rules! foo {
    ($($a:ident)? ; $num:expr) => { {
        let mut x = 0;

        $(
            x += $a;
         )?

        assert_eq!(x, $num);
    } }
}

pub fn main() {
    let a = 1;

    // accept 0 or 1 repetitions
    foo!( ; 0);
    foo!(a ; 1);
}

The current output is:

warning: variable does not need to be mutable
  --> src/main.rs:3:13
   |
3  |         let mut x = 0;
   |             ----^
   |             |
   |             help: remove this `mut`
...
17 |     foo!( ; 0);
   |     ---------- in this macro invocation
   |

cargo fix should perhaps not cross macro boundaries?
It will remove the mut inside the macro and the code will no longer work

The following errors were reported:
error[E0384]: cannot assign twice to immutable variable `x`
  --> src/main.rs:6:13
   |
3  |         let x = 0;
   |             -
   |             |
   |             first assignment to `x`
   |             help: consider making this binding mutable: `mut x`
...
6  |             x += $a;
   |             ^^^^^^^ cannot assign twice to immutable variable
...
18 |     foo!(a ; 1);
   |     ----------- in this macro invocation
   |
   = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0384`.
Original diagnostics will follow.
@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 1, 2022
@matthiaskrgr matthiaskrgr changed the title FP unused_mut macro FP unused_mut: macro Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

1 participant