Skip to content

SONARKT-739 S6518 Add class exceptions for common user errors where operator is provided via java interop#701

Merged
matthew-reid-sonarsource merged 10 commits intomasterfrom
SONARKT-739
Apr 15, 2026
Merged

SONARKT-739 S6518 Add class exceptions for common user errors where operator is provided via java interop#701
matthew-reid-sonarsource merged 10 commits intomasterfrom
SONARKT-739

Conversation

@matthew-reid-sonarsource
Copy link
Copy Markdown
Contributor

Part of

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented Apr 13, 2026

SONARKT-739

@matthew-reid-sonarsource matthew-reid-sonarsource marked this pull request as ready for review April 13, 2026 13:01
@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha Bot commented Apr 13, 2026

Summary

What changed: Rule S6518 now includes class-level exceptions for Java interop operators that should not be converted to indexed access syntax.

Previously, the rule aggressively flagged all get() and set() calls for replacement with [] syntax, including Java interop methods like Calendar.get() and CompletableFuture.get(). These are not intended to be indexed access—using calendar[YEAR] would be semantically wrong.

The fix adds logic to:

  1. Detect whether a get/set call is a Java method (using KaSymbolOrigin.JAVA_SOURCE / JAVA_LIBRARY)
  2. If it's Java interop, check if the type is in an allow-list of classes where indexed access is idiomatic (ByteBuffer, BitSet, AtomicIntegerArray, etc., plus any List or Map implementation)
  3. Only report violations for Java interop operators on allowed types; skip everything else

This eliminates ~40 false positives across test projects (Corda, IntelliJ Rust, Kotlin, etc.) while preserving legitimate warnings for actual collection types.

Why: Java has no operator overloading syntax for get/set, so Kotlin treats all Java get() and set() methods as operators via interop. Without this distinction, the rule penalizes correct Kotlin code when developers intentionally call domain-specific Java methods.

What reviewers should know

Start here: Review IndexedAccessCheck.kt lines 30–45 (the allow-list definition) and lines 88–103 (the check logic). These are the core logic changes.

Key decisions:

  • Allow-list design: Specific types (ByteBuffer, BitSet, atomic arrays, Android sparse arrays) plus any List/Map subtype. This balances precision (avoiding false positives on domain-specific types like Calendar) with coverage (catching actual collections).
  • Kotlin library handling: Types from compiled Kotlin libraries (Guava, OkHttp, user libraries) with real operator fun get/set are not Java interop—they're flagged even though semantically they're operators. This is correct because it encourages idiomatic Kotlin syntax.
  • Semantics dependency: The check uses KaSymbolOrigin to distinguish Java from Kotlin; without full semantics (e.g., no classpath), some concrete List/Map implementations (ArrayList, HashMap) may not be caught as false negatives—documented in test samples.

Test coverage notes:

  • IndexedAccessCheckSample.kt: Four separate test methods showing all cases (Java interop excluded, Java interop allowed, Kotlin library types, Java library types with clear Compliant/Noncompliant expectations).
  • IndexedAccessCheckSampleNoSemantics.kt: Mirrors the full sample but expects false negatives where the classpath is unavailable.
  • Ruling files: ~40 violations removed across multiple OSS projects now that Calendar/CompletableFuture and similar no longer raise.
  • JAR size adjusted from 53.4–53.9 MB to 53.5–54.0 MB to account for added symbol origin checks.

  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

sonar-review-alpha[bot]

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@petertrr petertrr left a comment

Choose a reason for hiding this comment

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

Good progress, but more examples with non-stdlib dependencies are needed.

sonar-review-alpha[bot]

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

@petertrr petertrr left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment thread sonar-kotlin-plugin/build.gradle.kts Outdated
@matthew-reid-sonarsource matthew-reid-sonarsource enabled auto-merge (squash) April 14, 2026 13:27
sonar-review-alpha[bot]

This comment was marked as resolved.

sonar-review-alpha[bot]

This comment was marked as outdated.

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! ✅

🗣️ Give feedback

@sonarqube-next
Copy link
Copy Markdown

@matthew-reid-sonarsource matthew-reid-sonarsource merged commit c11e5d1 into master Apr 15, 2026
6 of 7 checks passed
@matthew-reid-sonarsource matthew-reid-sonarsource deleted the SONARKT-739 branch April 15, 2026 14:55
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