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

redundant_closure_call inside macro mangles code #9957

Closed
matthiaskrgr opened this issue Nov 26, 2022 · 0 comments · Fixed by #9987
Closed

redundant_closure_call inside macro mangles code #9957

matthiaskrgr opened this issue Nov 26, 2022 · 0 comments · Fixed by #9987
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

Comments

@matthiaskrgr
Copy link
Member

Summary

.

Lint Name

redundant_closure_call

Reproducer

I tried this code:

// run-pass
// ignore-emscripten FIXME(#45351) hits an LLVM assert
// revisions: mir thir
// [thir]compile-flags: -Zthir-unsafeck

#![feature(repr_simd, platform_intrinsics, concat_idents)]
#![allow(non_camel_case_types)]

#[repr(simd)]
#[derive(Copy, Clone)]
struct i32x4(i32, i32, i32, i32);
#[repr(simd)]
#[derive(Copy, Clone)]
struct u32x4(pub u32, pub u32, pub u32, pub u32);
#[repr(simd)]
#[derive(Copy, Clone)]
struct f32x4(pub f32, pub f32, pub f32, pub f32);

extern "platform-intrinsic" {
    fn simd_eq<T, U>(x: T, y: T) -> U;
    fn simd_ne<T, U>(x: T, y: T) -> U;
    fn simd_lt<T, U>(x: T, y: T) -> U;
    fn simd_le<T, U>(x: T, y: T) -> U;
    fn simd_gt<T, U>(x: T, y: T) -> U;
    fn simd_ge<T, U>(x: T, y: T) -> U;
}

macro_rules! cmp {
    ($method: ident($lhs: expr, $rhs: expr)) => {{
        let lhs = $lhs;
        let rhs = $rhs;
        let e: u32x4 = concat_idents!(simd_, $method)($lhs, $rhs);
        // assume the scalar version is correct/the behaviour we want.
        assert!((e.0 != 0) == lhs.0 .$method(&rhs.0));
        assert!((e.1 != 0) == lhs.1 .$method(&rhs.1));
        assert!((e.2 != 0) == lhs.2 .$method(&rhs.2));
        assert!((e.3 != 0) == lhs.3 .$method(&rhs.3));
    }}
}
macro_rules! tests {
    ($($lhs: ident, $rhs: ident;)*) => {{
        $(
            (|| {
                cmp!(eq($lhs, $rhs));
                cmp!(ne($lhs, $rhs));

                // test both directions
                cmp!(lt($lhs, $rhs));
                cmp!(lt($rhs, $lhs));

                cmp!(le($lhs, $rhs));
                cmp!(le($rhs, $lhs));

                cmp!(gt($lhs, $rhs));
                cmp!(gt($rhs, $lhs));

                cmp!(ge($lhs, $rhs));
                cmp!(ge($rhs, $lhs));
            })();
            )*
    }}
}
fn main() {
    // 13 vs. -100 tests that we get signed vs. unsigned comparisons
    // correct (i32: 13 > -100, u32: 13 < -100).    let i1 = i32x4(10, -11, 12, 13);
    let i1 = i32x4(10, -11, 12, 13);
    let i2 = i32x4(5, -5, 20, -100);
    let i3 = i32x4(10, -11, 20, -100);

    let u1 = u32x4(10, !11+1, 12, 13);
    let u2 = u32x4(5, !5+1, 20, !100+1);
    let u3 = u32x4(10, !11+1, 20, !100+1);

    let f1 = f32x4(10.0, -11.0, 12.0, 13.0);
    let f2 = f32x4(5.0, -5.0, 20.0, -100.0);
    let f3 = f32x4(10.0, -11.0, 20.0, -100.0);

    unsafe {
        tests! {
            i1, i1;
            u1, u1;
            f1, f1;

            i1, i2;
            u1, u2;
            f1, f2;

            i1, i3;
            u1, u3;
            f1, f3;
        }
    }

    // NAN comparisons are special:
    // -11 (*)    13
    // -5        -100 (*)
    let f4 = f32x4(f32::NAN, f1.1, f32::NAN, f2.3);

    unsafe {
        tests! {
            f1, f4;
            f2, f4;
            f4, f4;
        }
    }
}

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:42:10
   |
42 |           $(
   |  __________^
43 | |             tests! {
44 | |             f1, f4;
45 | |             f2, f4;
46 | |             f4, f4;
47 | |         };
48 | |             )*
   | |_____________^

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

error: aborting due to previous error; 1 warning emitted

Original diagnostics will follow.

warning: try not to call a closure in the expression where it is declared
  --> src/main.rs:43:13
   |
43 | /             (|| {
44 | |                 cmp!(eq($lhs, $rhs));
45 | |                 cmp!(ne($lhs, $rhs));
46 | |
...  |
58 | |                 cmp!(ge($rhs, $lhs));
59 | |             })();
   | |________________^
...
79 | /         tests! {
80 | |             i1, i1;
81 | |             u1, u1;
82 | |             f1, f1;
...  |
90 | |             f1, f3;
91 | |         }
   | |_________- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call
   = note: `#[warn(clippy::redundant_closure_call)]` on by default
   = note: this warning originates in the macro `tests` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try doing something like
   |
43 ~             tests! {
44 +             i1, i1;
45 +             u1, u1;
46 +             f1, f1;
47 +
48 +             i1, i2;
49 +             u1, u2;
50 +             f1, f2;
51 +
52 +             i1, i3;
53 +             u1, u3;
54 +             f1, f3;
55 ~         };
   |

warning: try not to call a closure in the expression where it is declared
   --> src/main.rs:43:13
    |
43  | /             (|| {
44  | |                 cmp!(eq($lhs, $rhs));
45  | |                 cmp!(ne($lhs, $rhs));
46  | |
...   |
58  | |                 cmp!(ge($rhs, $lhs));
59  | |             })();
    | |________________^
...
100 | /         tests! {
101 | |             f1, f4;
102 | |             f2, f4;
103 | |             f4, f4;
104 | |         }
    | |_________- in this macro invocation
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call
    = note: this warning originates in the macro `tests` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try doing something like
    |
43  ~             tests! {
44  +             f1, f4;
45  +             f2, f4;
46  +             f4, f4;
47  ~         };
    |

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 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 26, 2022
@bors bors closed this as completed in 87963f0 Nov 30, 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant