Skip to content

fix: skip Kotlin operator convention imports in unused-import detection - #740

Open
ariancovac wants to merge 1 commit into
peteromallet:mainfrom
ariancovac:fix/kotlin-operator-import-false-positives
Open

fix: skip Kotlin operator convention imports in unused-import detection#740
ariancovac wants to merge 1 commit into
peteromallet:mainfrom
ariancovac:fix/kotlin-operator-import-false-positives

Conversation

@ariancovac

Copy link
Copy Markdown

Problem

Kotlin invokes convention (operator) functions implicitly by the compiler. The clearest case: import androidx.compose.runtime.getValue / setValue are required for Compose State/MutableState property delegation (val x by someState, var y by mutableStateOf(...)), yet the names never appear in the file text.

Textual unused-import detection cross-references the imported name against the file body, so it flagged these live imports as unused on every scan (in one real Compose project: 26 findings across 15 files). Removing them breaks compilation — the reviewer either "fixes" a false positive into a build break, or has to dismiss the same findings after every rescan.

Fix

  • Add an optional implicit_import_names: frozenset[str] field to TreeSitterLangSpec: names a language invokes implicitly by convention, which a textual reference search cannot rule out.
  • Populate it for KOTLIN_SPEC with the Kotlin convention-function table: property delegation (getValue, setValue, provideDelegate), unary/arithmetic/ranges, augmented assignments, containment/indexing/invocation/comparison (contains, get, set, invoke, compareTo, equals), iteration (iterator, next, hasNext), and infix bitwise (and, or, xor, shl, shr, ushr).
  • detect_unused_imports skips imported names present in that set. This trades a small amount of recall (a genuinely unused operator-named import goes unreported) for eliminating a false-positive class that directly invites build-breaking fixes — consistent with the detector's conservative stance elsewhere (e.g. skipping errorful parses).
  • Other languages are unaffected (the field defaults to empty).

Test

test_unused_imports_skip_implicit_convention_names: with an implicit_import_names-carrying spec, an unreferenced getValue import yields no findings; with a plain spec it is still flagged. Suite: 29 passed in test_treesitter_analysis_direct.py + test_phase_builders.py.

Kotlin invokes convention (operator) functions implicitly: importing
androidx.compose.runtime.getValue is required for property delegation
(val x by someState) even though the name never appears in the file
text. Textual unused-import detection flagged such live imports as
unused; removing them breaks compilation.

Add an implicit_import_names set to the tree-sitter language spec and
populate it for Kotlin with the convention-function table (delegation,
unary, arithmetic, augmented assignment, containment/indexing,
invocation, comparison, iteration, infix bitwise). Imports resolving to
those names are skipped by unused-import detection.
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.

1 participant