Skip to content

Extra return statements at end of function should not lower cognitive complexity #14422

Closed
@KonaeAkira

Description

@KonaeAkira

Summary

Spamming return statements at the end of a function lowers the function's cognitive complexity score, which should not happen.

Lint Name

cognitive_complexity

Reproducer

I tried this code:

// cognitive_complexity lint is triggered
#[clippy::cognitive_complexity = "1"]
fn foo() {
    for _ in 0..10 {
        println!("hello there");
    }
}

// cognitive_complexity lint is NOT triggered
#[clippy::cognitive_complexity = "1"]
fn bar() {
    for _ in 0..10 {
        println!("hello there");
    }
    return;
    return;
}

I expected to see this happen:

  • Lint should be triggered for both foo and bar.

Instead, this happened:

  • Lint only triggered for foo.

Version

rustc 1.87.0-nightly (00f245915 2025-02-26)
binary: rustc
commit-hash: 00f245915b0c7839d42c26f9628220c4f1b93bf6
commit-date: 2025-02-26
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions