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

FP implicit_return, returns inserted into macros #9891

Open
matthiaskrgr opened this issue Nov 20, 2022 · 0 comments
Open

FP implicit_return, returns inserted into macros #9891

matthiaskrgr opened this issue Nov 20, 2022 · 0 comments
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

implicit_return

Reproducer

I tried this code:

// Test that array subslice patterns are correctly handled in const evaluation.

// run-pass

#[derive(PartialEq, Debug, Clone)]
struct N(u8);

#[derive(PartialEq, Debug, Clone)]
struct Z;

macro_rules! n {
    ($($e:expr),* $(,)?) => {
        [$(N($e)),*]
    }
}

// This macro has an unused variable so that it can be repeated base on the
// number of times a repeated variable (`$e` in `z`) occurs.
macro_rules! zed {
    ($e:expr) => { Z }
}

macro_rules! z {
    ($($e:expr),* $(,)?) => {
        [$(zed!($e)),*]
    }
}

// Compare constant evaluation and runtime evaluation of a given expression.
macro_rules! compare_evaluation {
    ($e:expr, $t:ty $(,)?) => {{
        const CONST_EVAL: $t = $e;
        const fn const_eval() -> $t { $e }
        static CONST_EVAL2: $t = const_eval();
        let runtime_eval = $e;
        assert_eq!(CONST_EVAL, runtime_eval);
        assert_eq!(CONST_EVAL2, runtime_eval);
    }}
}

// Repeat `$test`, substituting the given macro variables with the given
// identifiers.
//
// For example:
//
// repeat! {
//     ($name); X; Y:
//     struct $name;
// }
//
// Expands to:
//
// struct X; struct Y;
//
// This is used to repeat the tests using both the `N` and `Z`
// types.
macro_rules! repeat {
    (($($dollar:tt $placeholder:ident)*); $($($values:ident),+);*: $($test:tt)*) => {
        macro_rules! single {
            ($($dollar $placeholder:ident),*) => { $($test)* }
        }
        $(single!($($values),+);)*
    }
}

fn main() {
    repeat! {
        ($arr $Ty); n, N; z, Z:
        compare_evaluation!({ let [_, x @ .., _] = $arr!(1, 2, 3, 4); x }, [$Ty; 2]);
        compare_evaluation!({ let [_, ref x @ .., _] = $arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
        compare_evaluation!({ let [_, x @ .., _] = &$arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
    }

    compare_evaluation!({ let [_, .., N(x)] = n!(1, 2, 3, 4); x }, u8);
    compare_evaluation!({ let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }, &'static u8);
    compare_evaluation!({ let [_, .., N(x)] = &n!(1, 2, 3, 4); x }, &'static u8);

}

I saw this happen:
cargo clippy --fix -- -Aclippy::all -Wclippy::implicit_return

    Checking clpy v0.1.0 (/tmp/clpy)
warning: failed to automatically apply fixes suggested by rustc to crate `clpy`

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

  * /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs
  * 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[E0572]: return statement outside of function body
  --> src/main.rs:74:25
   |
74 |     compare_evaluation!(return { let [_, .., N(x)] = n!(1, 2, 3, 4); x }, u8);
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0572]: return statement outside of function body
  --> src/main.rs:75:25
   |
75 |     compare_evaluation!(return { let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }, &'static u8);
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0572]: return statement outside of function body
  --> src/main.rs:76:25
   |
76 |     compare_evaluation!(return { let [_, .., N(x)] = &n!(1, 2, 3, 4); x }, &'static u8);
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
  --> src/main.rs:74:70
   |
74 |     compare_evaluation!(return { let [_, .., N(x)] = n!(1, 2, 3, 4); x }, u8);
   |                                                                      ^ expected `()`, found `u8`

warning: unreachable statement
  --> src/main.rs:74:5
   |
74 |     compare_evaluation!(return { let [_, .., N(x)] = n!(1, 2, 3, 4); x }, u8);
   |     ^^^^^^^^^^^^^^^^^^^^------------------------------------------------^^^^^
   |     |                   |
   |     |                   any code following this expression is unreachable
   |     unreachable statement
   |
   = note: `#[warn(unreachable_code)]` on by default
   = note: this warning originates in the macro `assert_eq` which comes from the expansion of the macro `compare_evaluation` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> src/main.rs:75:74
   |
75 |     compare_evaluation!(return { let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }, &'static u8);
   |                                                                          ^ expected `()`, found `&u8`

warning: unreachable statement
  --> src/main.rs:75:5
   |
75 |     compare_evaluation!(return { let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }, &'static u8);
   |     ^^^^^^^^^^^^^^^^^^^^----------------------------------------------------^^^^^^^^^^^^^^
   |     |                   |
   |     |                   any code following this expression is unreachable
   |     unreachable statement
   |
   = note: this warning originates in the macro `assert_eq` which comes from the expansion of the macro `compare_evaluation` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> src/main.rs:76:71
   |
76 |     compare_evaluation!(return { let [_, .., N(x)] = &n!(1, 2, 3, 4); x }, &'static u8);
   |                                                                       ^ expected `()`, found `&u8`

warning: unreachable statement
  --> src/main.rs:76:5
   |
76 |     compare_evaluation!(return { let [_, .., N(x)] = &n!(1, 2, 3, 4); x }, &'static u8);
   |     ^^^^^^^^^^^^^^^^^^^^-------------------------------------------------^^^^^^^^^^^^^^
   |     |                   |
   |     |                   any code following this expression is unreachable
   |     unreachable statement
   |
   = note: this warning originates in the macro `assert_eq` which comes from the expansion of the macro `compare_evaluation` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 6 previous errors; 3 warnings emitted

Some errors have detailed explanations: E0308, E0572.
For more information about an error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: missing `return` statement
  --> src/main.rs:69:29
   |
69 |         compare_evaluation!({ let [_, x @ .., _] = $arr!(1, 2, 3, 4); x }, [$Ty; 2]);
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `return` as shown: `return { let [_, x @ .., _] = $arr!(1, 2, 3, 4); x }`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return
   = note: requested on the command line with `-W clippy::implicit-return`
   = note: this warning originates in the macro `single` which comes from the expansion of the macro `repeat` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: missing `return` statement
  --> src/main.rs:70:29
   |
70 |         compare_evaluation!({ let [_, ref x @ .., _] = $arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `return` as shown: `return { let [_, ref x @ .., _] = $arr!(1, 2, 3, 4); x }`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return
   = note: this warning originates in the macro `single` which comes from the expansion of the macro `repeat` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: missing `return` statement
  --> src/main.rs:71:29
   |
71 |         compare_evaluation!({ let [_, x @ .., _] = &$arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `return` as shown: `return { let [_, x @ .., _] = &$arr!(1, 2, 3, 4); x }`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return
   = note: this warning originates in the macro `single` which comes from the expansion of the macro `repeat` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: missing `return` statement
  --> src/main.rs:69:29
   |
69 |         compare_evaluation!({ let [_, x @ .., _] = $arr!(1, 2, 3, 4); x }, [$Ty; 2]);
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `return` as shown: `return { let [_, x @ .., _] = $arr!(1, 2, 3, 4); x }`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return
   = note: this warning originates in the macro `single` which comes from the expansion of the macro `repeat` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: missing `return` statement
  --> src/main.rs:74:25
   |
74 |     compare_evaluation!({ let [_, .., N(x)] = n!(1, 2, 3, 4); x }, u8);
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `return` as shown: `return { let [_, .., N(x)] = n!(1, 2, 3, 4); x }`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return

warning: missing `return` statement
  --> src/main.rs:75:25
   |
75 |     compare_evaluation!({ let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }, &'static u8);
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `return` as shown: `return { let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return

warning: missing `return` statement
  --> src/main.rs:76:25
   |
76 |     compare_evaluation!({ let [_, .., N(x)] = &n!(1, 2, 3, 4); x }, &'static u8);
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `return` as shown: `return { let [_, .., N(x)] = &n!(1, 2, 3, 4); x }`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return

warning: `clpy` (bin "clpy") generated 9 warnings (2 duplicates) (run `cargo fix --bin "clpy"` to apply 3 suggestions)
warning: `clpy` (bin "clpy" test) generated 9 warnings (9 duplicates)
    Finished dev [unoptimized + debuginfo] target(s) in 0.32s

I expected to see this happen:

Version

rustc 1.67.0-nightly (c5d82ed7a 2022-11-19)
binary: rustc
commit-hash: c5d82ed7a4ad94a538bb87e5016e7d5ce0bd434b
commit-date: 2022-11-19
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 20, 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

No branches or pull requests

1 participant