Skip to content

FP unused_mut: macro #105149

@matthiaskrgr

Description

@matthiaskrgr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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

    Development

    No branches or pull requests

    Issue actions