Skip to content

Cargo fix will rename unused idents in macros even when used elsewhere #117284

Closed
@gfaster

Description

@gfaster

Problem

Consider the following src/main.rs file in a cargo package:

macro_rules! make_var {
    ($struct:ident, $var:ident) => {
        let $var = $struct.$var;
    };
}

#[allow(unused)]
struct MyStruct {
    var: i32,
}

fn main() {
    let s = MyStruct { var: 42 };
    make_var!(s, var);
}

Cargo will warn that var is not used. Upon running cargo fix, cargo will change the second parameter from var to _var, making the compilation fail. Even worse, if MyStruct contained a field _var then cargo fix would change the variable without producing any warning.

Steps

  1. Initialize a new cargo binary project with the above snippet in main.rs
  2. Run cargo fix (presumably with --allow-dirty)

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.73.0 (9c4383fb5 2023-08-26)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-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