Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KonaeAkira opened this issue Mar 17, 2025 · 0 comments · Fixed by #14460
Closed

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

KonaeAkira opened this issue Mar 17, 2025 · 0 comments · Fixed by #14460
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@KonaeAkira
Copy link
Contributor

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
@KonaeAkira KonaeAkira added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Mar 17, 2025
@KonaeAkira KonaeAkira changed the title Unreachable return statements at end of function should not lower cognitive complexity Extra return statements at end of function should not lower cognitive complexity Mar 17, 2025
github-merge-queue bot pushed a commit that referenced this issue Apr 12, 2025
…14460)

changelog: [`cognitive_complexity`]: Consecutive return calls decreased
complexity level of the function by 1.

fixes #14422
github-merge-queue bot pushed a commit that referenced this issue Apr 13, 2025
#14603)

changelog: [cognitive_complexity]: Changed Test for this issue from
making sure its not a false positive to making sure its not a false
negative which was the original issue at hand. The test as it was would
also not fail without the change introduced. This test will show if any
regression is made in further pushes.

adds tests to #14422
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant