Skip to content

fixed the issue of guard clauses#9424

Open
Phoenix1808 wants to merge 2 commits into
detekt:mainfrom
Phoenix1808:fix/guard_clauses
Open

fixed the issue of guard clauses#9424
Phoenix1808 wants to merge 2 commits into
detekt:mainfrom
Phoenix1808:fix/guard_clauses

Conversation

@Phoenix1808

@Phoenix1808 Phoenix1808 commented Jun 21, 2026

Copy link
Copy Markdown

Fixes #8993

Problem

When excludeGuardClauses = true then ReturnCount and ThrowsCount should not count guard clauses but if function was starting with normal local variable declaration then the guard clause coming after it was counted as well which results in false positive.

Root cause lies in file Junk.kt method yieldStatementsSkippingGuardClauses which keeps flag firstNonGuardFound. The moment statement isn't guard clause, this flag becomes true and after that every statement is counted. A local val/var declaration was treated as such a non-guard statement, so it was ending the guard section too early.

Fix

We generalized the exisiting isScopedAssignment helper into isLocalVariableDeclaration , earlier only val x = x was transparent now it's valid for any local var/val declaration. Also added the test cases to both ReturnCountSpec.kt and ThrowsCountSpec.kt for guard clauses interleaved with local variable declarations and one existing test updated.
The one updated test "(should not ignore scoped .... )" reflects this a differently named local like val data1= data no longer ends the guard section. This maybe slightly broadens #7931 @eygraber

Testing

First Reproduced bug as a failing test using issue's scenario before the fix it failed with same message from issue after the fix, new tests pass and all exisiting ReturnCount/ThrowsCount test still pass.

@schalkms

@codecov

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.73%. Comparing base (6bad2e8) to head (8936a49).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9424      +/-   ##
============================================
+ Coverage     84.72%   84.73%   +0.01%     
  Complexity     4492     4492              
============================================
  Files           571      571              
  Lines         12407    12410       +3     
  Branches       2767     2766       -1     
============================================
+ Hits          10512    10516       +4     
  Misses          694      694              
+ Partials       1201     1200       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReturnCount excludeGuardClauses false positives

2 participants