Skip to content

cargo clippy --fix returns compilation errors when fixing clippy::single_match lint #14493

Description

@bpandreotti

Summary

When trying to fix a clippy::single_match lint where the match value is a macro call, cargo clippy --fix is apparently trying to inline the macro, resulting in an error.

Reproducer

I ran cargo clippy --fix on the following code, which triggers the clippy::single_match lint:

macro_rules! foo {
    () => {
        $crate::bar()
    };
}

fn bar() -> Option<usize> {
    Some(42)
}

fn main() {
    match foo!() {
        Some(u) => println!("{}", u),
        None => (),
    }
}

Clippy was not able to automatically fix the lint. This was the full output:

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

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:12:22
   |
12 |     if let Some(u) = $crate::bar() { println!("{}", u) }
   |                      ^ expected expression

warning: unused macro definition: `foo`
 --> src/main.rs:1:14
  |
1 | macro_rules! foo {
  |              ^^^
  |
  = note: `#[warn(unused_macros)]` on by default

error: aborting due to 1 previous error; 1 warning emitted

Original diagnostics will follow.

warning: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
  --> src/main.rs:12:5
   |
12 | /     match foo!() {
13 | |         Some(u) => println!("{}", u),
14 | |         None => (),
15 | |     }
   | |_____^ help: try: `if let Some(u) = $crate::bar() { println!("{}", u) }`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match
   = note: `#[warn(clippy::single_match)]` on by default

warning: `clippy-bug` (bin "clippy-bug") generated 1 warning (run `cargo clippy --fix --bin "clippy-bug"` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `clippy_bug`

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:12:22
   |
12 |     if let Some(u) = $crate::bar() { println!("{}", u) }
   |                      ^ expected expression

warning: unused macro definition: `foo`
 --> src/main.rs:1:14
  |
1 | macro_rules! foo {
  |              ^^^
  |
  = note: `#[warn(unused_macros)]` on by default

error: aborting due to 1 previous error; 1 warning emitted

Original diagnostics will follow.

warning: `clippy-bug` (bin "clippy-bug" test) generated 1 warning (1 duplicate)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.07s

Version

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-unknown-linux-gnu
release: 1.85.1
LLVM version: 19.1.7

Additional Labels

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions