Skip to content

SONARKT-681 S1862 should not raise when guard conditions are used in "when" statements#689

Merged
petertrr merged 2 commits intomasterfrom
SONARKT-681
Apr 15, 2026
Merged

SONARKT-681 S1862 should not raise when guard conditions are used in "when" statements#689
petertrr merged 2 commits intomasterfrom
SONARKT-681

Conversation

@petertrr
Copy link
Copy Markdown
Contributor

Summary

Fixed S1862 rule to not raise when guard conditions are used in "when" statements in Kotlin.

Changes

  • Updated S1862 inspection rule logic to properly handle guard conditions in when statements
  • Added test cases to verify the fix works correctly

Testing

  • Ran local tests to verify the fix
  • Tested with sample code containing guard conditions in when statements

Ticket

SONARKT-681

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented Mar 26, 2026

SONARKT-681

@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha Bot commented Mar 26, 2026

Summary

The PR refines the S1862 rule (identical conditions) to properly handle guard conditions in Kotlin's when expressions. Previously, the rule would incorrectly flag identical patterns with different guards as duplicates. The fix introduces a new ConditionWithGuard data structure and a groupEquivalent() grouping function that treats two when entries as duplicates only when both their condition patterns AND their guard expressions are syntactically equivalent. This prevents false positives when the same pattern is guarded by different conditions.

What reviewers should know

Start with the test cases in IdenticalConditionsCheckSample.kt — the new test block starting at line 32 shows the fix in action with concrete examples. Notice how true if a == "foo" followed by true if a == "bar" are now compliant (no warning), but identical pattern+guard pairs still trigger the noncompliant warning.

Core logic is in checkWhenConditions() (replacing the old collectConditions()). The key change: instead of comparing raw conditions, the method now wraps conditions with their optional guard expressions in a ConditionWithGuard object, then groups using a custom predicate that checks both the condition and guard for syntactic equivalence. Review the predicate logic around line 73-77 — this is where the fix happens.

groupEquivalent() is a new helper that groups elements by a custom equivalence predicate. It's a straightforward implementation but worth understanding since it replaces the previous use of SyntacticEquivalence.findDuplicatedGroups() with custom logic.

Minor cleanup: SyntacticEquivalence.kt changes isNullOrEmpty() to isEmpty() (lists from .filterIsInstance() are never null).


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

sonar-review-alpha[bot]

This comment was marked as outdated.

@sonarqube-next
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

Solid fix. The guard-aware deduplication logic is correct, line numbers in // Noncompliant annotations are accurate, and the existing if-chain path is untouched.

🗣️ Give feedback

Copy link
Copy Markdown
Contributor

@matthew-reid-sonarsource matthew-reid-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@petertrr petertrr merged commit 1a7c44f into master Apr 15, 2026
11 checks passed
@petertrr petertrr deleted the SONARKT-681 branch April 15, 2026 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants