rust-lang / rust-lang/rust-clippy

FP implicit_return, returns inserted into macros

Open
#9,891 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the implicit_return lint and reproduce the report using the cargo clippy command shown against the src/main.rs example. Inspect how the lint handles expressions originating in macro expansions, including the reported core/src/macros/mod.rs location. Done means the fix no longer inserts return statements into macro arguments and the reproduced code compiles without the listed errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.