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

Bug Report: Clippy needless_return incorrectly gets triggered #10546

Closed
vaporoxx opened this issue Mar 25, 2023 · 3 comments · Fixed by #10593
Closed

Bug Report: Clippy needless_return incorrectly gets triggered #10546

vaporoxx opened this issue Mar 25, 2023 · 3 comments · Fixed by #10593
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

@vaporoxx
Copy link

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
@vaporoxx vaporoxx added the C-bug Category: Clippy is not doing the correct thing label Mar 25, 2023
@jyn514
Copy link
Member

jyn514 commented Mar 25, 2023

You should report this in rust-lang/rust-clippy.

cc rust-lang/cargo#11877

@vaporoxx
Copy link
Author

Could this issue maybe be transferred over to that repository then?

@jyn514
Copy link
Member

jyn514 commented Mar 25, 2023

I don't have permissions unfortunately.

@matthiaskrgr matthiaskrgr transferred this issue from rust-lang/rust Mar 25, 2023
@Alexendoo Alexendoo added 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 Mar 25, 2023
@bors bors closed this as completed in e22019d Apr 10, 2023
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.

3 participants