Skip to content

unused_must_use: comparison inside macro will cause invalid code to be suggested #104913

@matthiaskrgr

Description

@matthiaskrgr

Given the following code:

macro_rules! negative {
      ($e:expr) => { $e < 0 }
}

fn main() {
      negative!(1 as i32);
}

The current output is:

warning: unused comparison that must be used
 --> src/main.rs:2:22
  |
2 |       ($e:expr) => { $e < 0 }
  |                      ^^^^^^ the comparison produces a value
...
6 |       negative!(1 as i32);
  |       ------------------- in this macro invocation
  |
  = note: `#[warn(unused_must_use)]` on by default
  = note: this warning originates in the macro `negative` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `let _ = ...` to ignore the resulting value
  |
2 |       ($e:expr) => { let _ = $e < 0 }
  |                      +++++++

However, this does make it worse:

The following errors were reported:
error: macro expansion ends with an incomplete expression: expected one of `.`, `;`, `?`, `else`, or an operator
 --> src/main.rs:2:36
  |
2 |       ($e:expr) => { let _ = $e < 0 }
  |                                    ^ expected one of `.`, `;`, `?`, `else`, or an operator

error: aborting due to previous error

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