Skip to content

partial macro expansion in suggestion for if_not_else lint #15924

@m4rch3n1ng

Description

@m4rch3n1ng

Summary

doing an if !matches!( with clippy::if_not_else enabled (implied when clippy::pedantic is enabled), partially expands the macro and gives an incorrect suggestion.

Reproducer

I tried this code:

fn main() {
    let x = 0;
    if !matches!(x, 0..10) {
        println!(":)");
    } else {
        println!(":(");
    }
}

I expected to see this happen:

warning: unnecessary boolean `not` operation
 --> src/main.rs:3:5
  |
3 | /     if !matches!(x, 0..10) {
4 | |         println!(":)");
5 | |     } else {
6 | |         println!(":(");
7 | |     }
  | |_____^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_not_else
  = note: `-W clippy::if-not-else` implied by `-W clippy::pedantic`
  = help: to override `-W clippy::pedantic` add `#[allow(clippy::if_not_else)]`
help: try
  |
3 ~     if matches!(x, 0..10) {
4 +         println!(":(");
5 +     } else {
6 +         println!(":)");
7 +     }
  |

Instead, this happened:

warning: unnecessary boolean `not` operation
 --> src/main.rs:3:5
  |
3 | /     if !matches!(x, 0..10) {
4 | |         println!(":)");
5 | |     } else {
6 | |         println!(":(");
7 | |     }
  | |_____^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_not_else
  = note: `-W clippy::if-not-else` implied by `-W clippy::pedantic`
  = help: to override `-W clippy::pedantic` add `#[allow(clippy::if_not_else)]`
help: try
  |
3 ~     if match $expression {
4 +             $pattern $(if $guard)? => true,
5 +             _ => false
6 +         } {
7 +         println!(":(");
8 +     } else {
9 +         println!(":)");
10+     }
  |

trying to apply this suggestion with --fix causes this:

warning: failed to automatically apply fixes suggested by rustc to crate `tmp`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected expression, found `$`
 --> src/main.rs:3:14
  |
3 |     if match $expression {
  |        ----- ^ expected expression
  |        |
  |        while parsing this `match` expression

error: aborting due to 1 previous error

Version

rustc 1.92.0-nightly (53a741fc4 2025-10-16)
binary: rustc
commit-hash: 53a741fc4b8cf2d8e7b1b2336ed8edf889db84f4
commit-date: 2025-10-16
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.3

Additional Labels

@rustbot label +L-suggestion +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions