Skip to content

Needlessly Verbose Replacement Suggestion for needless_return #14750

Open
@fennewald

Description

@fennewald

Summary

The needless_return lint will report suggest removing and re-adding the preceding line, rather than ignoring it. As you can see from the example below, the reported span of the return statement seems to start at the newline following the semicolon of println!(). This generates an uglier warning, and a somewhat nonsensical help suggestion, suggesting I delete and then recreate the println statement without changing anything.

Reproducer

I tried this code:

fn main() {
    if true {
        println!("foo");
        return;
    } else {
        println!("bar");
    }
}

I expected to see this happen:

 --> src/main.rs:3:25
  |
3 |           println!("foo");
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: remove `return`
  |
3 |         println!("foo");
4 -         return;
  |

Instead, this happened:

 --> src/main.rs:3:25
  |
3 |           println!("foo");
  |  _________________________^
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: remove `return`
  |
3 -         println!("foo");
4 -         return;
3 +         println!("foo");
  |

Version

rustc 1.88.0-nightly (27d6200a7 2025-05-06)
binary: rustc
commit-hash: 27d6200a70601f6fcf419bf2f9e37989f3624ca4
commit-date: 2025-05-06
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.4

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions