fix: skip Kotlin operator convention imports in unused-import detection - #740
Open
ariancovac wants to merge 1 commit into
Open
fix: skip Kotlin operator convention imports in unused-import detection#740ariancovac wants to merge 1 commit into
ariancovac wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Kotlin invokes convention (operator) functions implicitly by the compiler. The clearest case:
import androidx.compose.runtime.getValue/setValueare required for ComposeState/MutableStateproperty 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
implicit_import_names: frozenset[str]field toTreeSitterLangSpec: names a language invokes implicitly by convention, which a textual reference search cannot rule out.KOTLIN_SPECwith 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_importsskips 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).Test
test_unused_imports_skip_implicit_convention_names: with animplicit_import_names-carrying spec, an unreferencedgetValueimport yields no findings; with a plain spec it is still flagged. Suite: 29 passed intest_treesitter_analysis_direct.py+test_phase_builders.py.