Skip to content

Bug Report: Clippy needless_return incorrectly gets triggered #10546

Closed
@vaporoxx

Description

@vaporoxx

When using a match statement at the end of a function that has a return statement in a match arm and is followed by a semicolon, cargo clippy will suggest replacing the return with a unit value. However, it does this regardless of what type the match statement actually returns, so if it is something other than unit, it breaks.

Minimal reproducible example:

fn main() {
    match 0 {
        0 => 0,
        _ => return,
    };
}

cargo clippy --fix:

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

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[E0308]: `match` arms have incompatible types
 --> src/main.rs:4:14
  |
2 | /     match 0 {
3 | |         0 => 0,
  | |              - this is found to be of type `{integer}`
4 | |         _ => (),
  | |              ^^ expected integer, found `()`
5 | |     }
  | |_____- `match` arms have incompatible types

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: unneeded `return` statement
 --> src/main.rs:4:14
  |
4 |         _ => return,
  |              ^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
  = note: `#[warn(clippy::needless_return)]` on by default
  = help: replace `return` with a unit value

warning: `test` (bin "test") generated 1 warning (run `cargo clippy --fix --bin "test"` to apply 1 suggestion)
warning: `test` (bin "test" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.18s

rustc --version --verbose:

rustc 1.70.0-nightly (8be3c2bda 2023-03-24)
binary: rustc
commit-hash: 8be3c2bda6b683f87b24714ba595e8b04faef54c
commit-date: 2023-03-24
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 15.0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-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