Skip to content

Commit

Permalink
Migrate UnusedLocalVariableCheck to kotlin-analysis-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Feb 6, 2025
1 parent 4242471 commit f1b2295
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ import org.sonarsource.kotlin.api.checks.AbstractCheck
import org.sonarsource.kotlin.api.checks.getVariableType
import org.sonarsource.kotlin.api.frontend.KotlinFileContext

@org.sonarsource.kotlin.api.frontend.K1only
@Rule(key = "S1481")
class UnusedLocalVariableCheck : AbstractCheck() {

override fun visitKtFile(file: KtFile, context: KotlinFileContext) {
context.diagnostics
context.kaDiagnostics
.filter {
it.factory == Errors.UNUSED_VARIABLE &&
it.psiElement.getVariableType(context.bindingContext)?.getKotlinTypeFqName(false) != "kotlin.Nothing"
it.factoryName == Errors.UNUSED_VARIABLE.name &&
// TODO remove after switch to K2:
(it.psi as KtNamedDeclaration).getVariableType(context.bindingContext)
?.getKotlinTypeFqName(false) != "kotlin.Nothing"
}
.map { it.psiElement as KtNamedDeclaration }
.map { it.psi as KtNamedDeclaration }
.forEach {
context.reportIssue(it.nameIdentifier!!, """Remove this unused "${it.name}" local variable.""")
}
Expand Down

0 comments on commit f1b2295

Please sign in to comment.