Skip to content

needless_return false positive when combined with cfg'd code afterwards #14474

Open
@DianaNites

Description

@DianaNites

Summary

The needless_return lint does not recognize cfg'd out code located after the return, which makes the return actually required, and where removing it is invalid and will fail to compile.

Lint Name

needless_return

Reproducer

I tried this code:

fn _bar() -> u64 {
    return 456;
    
    #[cfg(no)]
    123
}

I saw this happen:

warning: unneeded `return` statement
 --> src/lib.rs:17:5
   |
17 |     return 456;
   |     ^^^^^^^^^^

I expected to see this happen:

No warning. Following the suggestion will fail to compile, and give further incorrect advice.

error: expected `;`, found `#`
  --> src/lib.rs:17:8
   |
17 |     456
   |        ^ help: add `;` here
18 |     
19 |     #[cfg(no)]
   |     - unexpected token

Version

rustc 1.87.0-nightly (1aeb99d24 2025-03-19)
binary: rustc
commit-hash: 1aeb99d248e1b0069110cb03c6f1dcc7b36fd7f3
commit-date: 2025-03-19
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Additional Labels

No response

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