Skip to content

cargo clippy --fix fails to fix unneeded return #9192

@Seeker14491

Description

@Seeker14491

Running cargo clippy --fix fails to fix the following code:

fn f() -> i32 {
    {
        return 0;
    };
}

It tries to convert the return 0; into an implicit return, but it fails to remove the semicolon on the following line, which causes the error.

Full Output

$ cargo clippy --fix
    Checking rustfmt-debug v0.1.0 (/home/seekr/Desktop/rustfmt-debug)
warning: failed to automatically apply fixes suggested by rustc to crate `rustfmt_debug`

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

  * src/lib.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]: mismatched types
 --> src/lib.rs:1:15
  |
1 | pub fn f() -> i32 {
  |        -      ^^^ expected `i32`, found `()`
  |        |
  |        implicitly returns `()` as its body has no tail or `return` expression
...
4 |     };
  |      - help: remove this semicolon

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/lib.rs:3:9
  |
3 |         return 0;
  |         ^^^^^^^^^ help: remove `return`: `0`
  |
  = note: `#[warn(clippy::needless_return)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return

warning: `rustfmt-debug` (lib) generated 1 warning (1 duplicate)
warning: `rustfmt-debug` (lib test) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s

Meta

rustc --version --verbose:

rustc 1.64.0-nightly (c2f428d2f 2022-07-14)
binary: rustc
commit-hash: c2f428d2f3340a0e7d995f4726223db91b93704c
commit-date: 2022-07-14
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-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