Skip to content

Feature request: Detect uses of $ in macro_rules! that could be mistyped metavariables #141826

Open
@schuelermine

Description

@schuelermine

Code

macro_rules! call_method {
    ($val: expr, $met: ident) => {
        $val.$meth()
    }
}

fn main() {
    let data = vec![10, 20];
    let length = call_method!(data, len);
}

Current output

Compiling playground v0.0.1 (/playground)
error: unexpected token: `$`
 --> src/main.rs:3:14
  |
3 |         $val.$meth()
  |              ^^^^^
...
9 |     let length = call_method!(data, len);
  |                  ----------------------- in this macro invocation
  |
  = note: this error originates in the macro `call_method` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores `$` and any tokens following
 --> src/main.rs:3:14
  |
3 |         $val.$meth()
  |              ^^^^^
...
9 |     let length = call_method!(data, len);
  |                  ----------------------- caused by the macro expansion here
  |
  = note: the usage of `call_method!` is likely invalid in expression context

warning: unused variable: `length`
 --> src/main.rs:9:9
  |
9 |     let length = call_method!(data, len);
  |         ^^^^^^ help: if this is intentional, prefix it with an underscore: `_length`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 2 previous errors; 1 warning emitted

Desired output

Compiling playground v0.0.1 (/playground)
error: unexpected token: `$`
 --> src/main.rs:3:14
  |
3 |         $val.$meth()
  |              ^^^^^ help: a metavariable with a similar name exists: `$met`
...
9 |     let length = call_method!(data, len);
  |                  ----------------------- in this macro invocation
  |
help: `$` in `macro_rules!` macros is treated as a literal `$` if the following identifier is not the name of a metavariable
  = note: this error originates in the macro `call_method` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores `$` and any tokens following
 --> src/main.rs:3:14
  |
3 |         $val.$meth()
  |              ^^^^^
...
9 |     let length = call_method!(data, len);
  |                  ----------------------- caused by the macro expansion here
  |
  = note: the usage of `call_method!` is likely invalid in expression context

warning: unused variable: `length`
 --> src/main.rs:9:9
  |
9 |     let length = call_method!(data, len);
  |         ^^^^^^ help: if this is intentional, prefix it with an underscore: `_length`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 2 previous errors; 1 warning emitted

Rationale and extra context

No response

Other cases

Rust Version

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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