Skip to content

Compile error after applying clippy::identity_op #13924

Open
@dhedey

Description

@dhedey

Summary

I was writing tests for preinterpet on this draft PR and encountered the following error after running clippy:

after fixes were automatically applied the compiler reported errors
...
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!
...
clippy::identity_op

The code

The code which tripped up clippy was a little artificial, as it was a test case for building partial expressions from token streams inside macros. In particular, it was the following code:

    assert_preinterpret_eq!({
        0
        [!if! true { + 1 }]
    }, 1);

The full error

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

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

  * tests/control_flow.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: mismatched closing delimiter: `]`
  --> tests/control_flow.rs:21:28
   |
21 |     assert_preinterpret_eq!({
   |                            ^ unclosed delimiter
22 |         1 }]
   |            ^ mismatched closing delimiter

error: unexpected closing delimiter: `)`
  --> tests/control_flow.rs:23:9
   |
22 |         1 }]
   |            - missing open `[` for this delimiter
23 |     }, 1);
   |         ^ unexpected closing delimiter

error: aborting due to 2 previous errors

Original diagnostics will follow.

warning: this operation has no effect
  --> tests/control_flow.rs:22:9
   |
22 | /         0
23 | |         [!if! true { + 1 }]
   | |________________________^ help: consider reducing it to: `1`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
   = note: `#[warn(clippy::identity_op)]` on by default

warning: `preinterpret` (test "control_flow") generated 1 warning (run `cargo clippy --fix --test "control_flow"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.43s

Reproducer

I tried this code:

    assert_preinterpret_eq!({
        0
        [!if! true { + 1 }]
    }, 1);

Where the assert_preinterpret_eq! macro was defined as:

macro_rules! assert_preinterpret_eq {
    ($input:tt, $($output:tt)*) => {
        assert_eq!(preinterpret!($input), $($output)*);
    };
}

In this WIP branch in this repo.

You can probably substitute the following to reproduce the issue, without using preinterpret:

macro_rules! assert_preinterpret_eq {
    ($($output:tt)*) => {};
}

I expected to see this happen: Clippy ignored it

Instead, this happened: Clippy attempted to fix it and got a compile error.

Version

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7

Additional Labels

@rustbot label +l-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions