Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unnecessary_operation destroyes code inside macros #9954

Closed
matthiaskrgr opened this issue Nov 25, 2022 · 0 comments · Fixed by #9981
Closed

unnecessary_operation destroyes code inside macros #9954

matthiaskrgr opened this issue Nov 25, 2022 · 0 comments · Fixed by #9981
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied T-macros Type: Issues with macros and macro expansion

Comments

@matthiaskrgr
Copy link
Member

Summary

.

Lint Name

unnecessary_operation

Reproducer

I tried this code:

#![allow(unused_must_use)]
#![feature(rustc_attrs)]

use std::thread;

trait Int {
    fn zero() -> Self;
    fn one() -> Self;
}
macro_rules! doit {
    ($($t:ident)*) => ($(impl Int for $t {
        fn zero() -> Self { 0 }
        fn one() -> Self { 1 }
    })*)
}
doit! { i8 isize }

macro_rules! check {
    ($($e:expr),*) => {
        $(assert!(thread::spawn({
            move|| { $e; } // RIP
        }).join().is_err());)*
    }
}

fn main() {
    check![
        isize::MIN / -isize::one(),
        i8::MIN / -i8::one()
    ];
}

I saw this happen:
cargo clippy ---fix

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/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: attempted to repeat an expression containing no syntax variables matched as repeating at this depth
  --> src/main.rs:20:10
   |
20 |           $(assert!(thread::spawn({
   |  __________^
21 | |             move|| { i8::MIN;-i8::one(); } // RIP
22 | |         }).join().is_err());)*
   | |_____________________________^

warning: unused import: `std::thread`
 --> src/main.rs:4:5
  |
4 | use std::thread;
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error: aborting due to previous error; 1 warning emitted

Original diagnostics will follow.

warning: unnecessary operation
  --> src/main.rs:21:22
   |
21 |               move|| { $e; } // RIP
   |                        ^^^ help: statement can be reduced to: `isize::MIN;-isize::one();`
...
27 | /     check![
28 | |         isize::MIN / -isize::one(),
29 | |         i8::MIN / -i8::one()
30 | |     ];
   | |_____- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
   = note: `#[warn(clippy::unnecessary_operation)]` on by default
   = note: this warning originates in the macro `check` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: unnecessary operation
  --> src/main.rs:21:22
   |
21 |               move|| { $e; } // RIP
   |                        ^^^ help: statement can be reduced to: `i8::MIN;-i8::one();`
...
27 | /     check![
28 | |         isize::MIN / -isize::one(),
29 | |         i8::MIN / -i8::one()
30 | |     ];
   | |_____- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
   = note: this warning originates in the macro `check` (in Nightly builds, run with -Z macro-backtrace for more info)


Version

rustc 1.67.0-nightly (b3bc6bf31 2022-11-24)
binary: rustc
commit-hash: b3bc6bf31265ac10946a0832092dbcedf9b26805
commit-date: 2022-11-24
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing T-macros Type: Issues with macros and macro expansion I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 25, 2022
@bors bors closed this as completed in 4cda21d Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant