Skip to content

[kotlin] Fix #7092: Add LooseCoupling rule - #7094

Merged
adangel merged 5 commits into
pmd:mainfrom
stokpop:kotlin/loose-coupling-rule
Oct 23, 2026
Merged

adangel merged 5 commits into
pmd:mainfrom
stokpop:kotlin/loose-coupling-rule

Conversation

@stokpop

@stokpop stokpop commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7092

Adds a LooseCoupling rule to pmd-kotlin, ported from pmd-java's LooseCouplingRule. Implemented as a plain XPath rule using the pmd-kotlin:typeIs()/typeIsExactly() functions and @TypeName/@ReturnTypeName/modifiers() attributes added in #6893.

What it flags

Concrete collection/map implementation types (ArrayList, LinkedList, Vector, Stack, HashMap, Hashtable, TreeMap, HashSet, LinkedHashSet, TreeSet) used for properties, class/function parameters, and function return types, in favor of the corresponding interface.

  • Uses typeIs() (subtype-aware), so user-defined subclasses are caught too.
  • Skips local variables, override-annotated members, and secondary-constructor parameters (not part of the API surface / not currently type-annotated).
  • Has an allowedTypes property (default java.util.Properties), mirroring the Java rule.

18 test cases; also validated against a real-world ~525-file Kotlin codebase, finding genuine violations at correct line numbers.

Known limitations (tracked in #7091, not blocking)

  • Checks a fixed list of JDK types rather than generically flagging any concrete Collection/Map implementation like pmd-java's rule does (would need a new isInterface()-style function).
  • Message is static (no {0} type-name substitution) — XPathRule's {0} auto-binding doesn't apply cleanly to Kotlin nodes; documented in docs/pages/pmd/languages/kotlin.md.

Related

Ports pmd-java's LooseCoupling rule to Kotlin as an XPath rule, flagging
concrete collection/map implementation types (ArrayList, HashMap,
LinkedHashMap, HashSet, LinkedHashSet, TreeMap, TreeSet, Vector, Stack,
Hashtable, LinkedList) used for properties, class/function parameters,
and function return types, in favor of the corresponding interface.

Demonstrates the new pmd-kotlin:typeIs() / typeIsExactly() / modifiers()
XPath functions and @typename / @ReturnTypeName attributes added in pmd#6893.

- Uses typeIs() (subtype-aware) so user-defined subclasses of a concrete
  collection type are also caught.
- Skips local variables (not part of the API surface), including those
  declared inside init {} blocks and secondary constructors, which use a
  plain Block rather than a FunctionBody.
- Skips override-annotated members, matching the Java rule's behavior.
- Skips secondary-constructor parameters, since they aren't currently
  visited by the type-annotation visitor and would otherwise fall back to
  a line-tolerant type lookup that can misattribute an adjacent
  declaration's type.
- Supports an allowedTypes property (default java.util.Properties),
  mirroring the Java rule's property of the same name.

Fixes pmd#7092
- Message no longer suggests Mutable* as an equal alternative to the
  read-only interface; that nuance now lives only in the description,
  worded to prefer the read-only interface by default and fall back to
  Mutable* only when mutation is actually needed.
- Description now documents that this rule checks a fixed list of
  common JDK concrete types, unlike pmd-java's LooseCouplingRule, which
  generically flags any concrete Collection/Map implementation. Doing
  the same generically in Kotlin isn't currently possible: ktm only
  exposes interface-vs-class info for declarations found in the
  analyzed Kotlin source, not for external/JDK types resolved via
  classpath. Tracked as a follow-up in pmd#7091.

Deliberately not growing the fixed type list piecemeal with popular
library collection types (Guava Multimap/BiMap/Immutable*, Apache
Commons Collections4, kotlinx.collections.immutable Persistent*,
etc.): several of those are often intentionally exposed as concrete
types to guarantee immutability, so hand-picking more now would create
debatable false positives. Prefer waiting for the generic
isInterface-based check (pmd#7091) to decide "concrete vs. interface" for
any library, rather than expanding this list ad hoc. (Note for PR
description once opened.)
@stokpop
stokpop force-pushed the kotlin/loose-coupling-rule branch from 101aea9 to a0b1869 Compare September 22, 2026 20:13
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-10-23 12:55:49+00:00 for 09ff99a)

@stokpop
stokpop marked this pull request as ready for review September 23, 2026 05:40
@adangel adangel changed the title [kotlin] Add LooseCoupling rule [kotlin] Fix #7092: Add LooseCoupling rule Oct 23, 2026
@adangel adangel added the a:new-rule Proposal to add a new built-in rule label Oct 23, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, looks good!

I'll update it with the current main and merge it for 7.29.0


### Message parameter (`{0}`) substitution

`net.sourceforge.pmd.lang.rule.xpath.XPathRule` auto-binds `{0}` in a rule's `message` from the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FYI - this is a known limitation, see also #4787

Comment thread docs/pages/release_notes.md Resolved
@adangel adangel added this to the 7.29.0 milestone Oct 23, 2026
@adangel
adangel merged commit 8d4094b into pmd:main Oct 23, 2026
13 checks passed
@stokpop

stokpop commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, looks good!

I'll update it with the current main and merge it for 7.29.0

@adangel Thanks for taking the time for reviewing the kotlin related PRs! Much appreciated

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

Labels

a:new-rule Proposal to add a new built-in rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[kotlin] New Rule: LooseCoupling

2 participants