Skip to content

Commit

Permalink
Rewrite UnnecessaryImportsCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
leveretka committed Nov 25, 2024
1 parent b71974a commit aa6621d
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package org.sonarsource.kotlin.checks

import org.jetbrains.kotlin.analysis.api.KaIdeApi
import org.jetbrains.kotlin.analysis.api.descriptors.KaFe10Session
import org.jetbrains.kotlin.descriptors.VariableAccessorDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors
import org.jetbrains.kotlin.descriptors.accessors
Expand Down Expand Up @@ -51,18 +53,37 @@ import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.sonar.check.Rule
import org.sonarsource.kotlin.api.checks.AbstractCheck
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
import org.sonarsource.kotlin.api.visiting.analyze

private const val MESSAGE_UNUSED = "Remove this unused import."
private const val MESSAGE_REDUNDANT = "Remove this redundant import."
private val DELEGATES_IMPORTED_NAMES = setOf("getValue", "setValue", "provideDelegate")
private val ARRAY_ACCESS_IMPORTED_NAMES = setOf("get", "set")

@org.sonarsource.kotlin.api.frontend.K1only("complex")
@org.sonarsource.kotlin.api.frontend.K1only("rewritten, results differ, fixed FNs, some issue disappeared, consistent with IDE")
@Rule(key = "S1128")
class UnnecessaryImportsCheck : AbstractCheck() {

@OptIn(KaIdeApi::class)
override fun visitKtFile(file: KtFile, context: KotlinFileContext) {

analyze {
if (this !is KaFe10Session) {
val analyzeImportsToOptimize = analyzeImportsToOptimize(file)

file.importDirectives.mapNotNull { import -> import.importedFqName?.let { import to it } }
.filter { (_, fqName: FqName) ->
!analyzeImportsToOptimize.unresolvedNames.contains(fqName.shortName()) &&
analyzeImportsToOptimize.usedDeclarations[fqName].isNullOrEmpty()
}.map { it.first }
.forEach { importDirective ->
// We could not find any usages for anything remaining at this point. Hence, report!
importDirective.importedReference?.let { context.reportIssue(it, MESSAGE_UNUSED) }
}
return
}
}

val (references, arrayAccesses, kDocLinks, delegateImports, calls) = collectReferences(file)

val bindingContext = context.bindingContext
Expand Down

0 comments on commit aa6621d

Please sign in to comment.