Skip to content

needless_return gives confusing advice #10816

@kpreid

Description

@kpreid

Summary

needless_return tells the user to remove the return but does not tell them to remove the accompanying semicolon, resulting in a broken program.

Reproducer

I ran cargo clippy on this code:

fn foo() -> i32 {
    return 100;
}

I expected to see this happen: A message that guides me to change the body to { 100 }.

Instead, this happened: A message that guides me to change the body to { 100; }:

warning: unneeded `return` statement
 --> src/lib.rs:2:5
  |
2 |     return 100;
  |     ^^^^^^^^^^
  |
  = 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: remove `return`

Note: this is only a problem with the user-visible message. cargo clippy --fix does produce a correct program.

Version

rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-apple-darwin
release: 1.69.0
LLVM version: 15.0.7

rustc 1.71.0-nightly (2f2c438dc 2023-05-08)
binary: rustc
commit-hash: 2f2c438dce75d8cc532c3baa849eeddc0901802c
commit-date: 2023-05-08
host: x86_64-apple-darwin
release: 1.71.0-nightly
LLVM version: 16.0.2

Additional Labels

@rustbot label +I-suggestion-causes-error

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