Skip to content

Commit

Permalink
Migrate ReasonableTypeCastsCheck 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 d0c792e commit 4d96420
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@
*/
package org.sonarsource.kotlin.checks

import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.psi.KtFile
import org.sonar.check.Rule
import org.sonarsource.kotlin.api.checks.AbstractCheck
import org.sonarsource.kotlin.api.frontend.KotlinFileContext

@org.sonarsource.kotlin.api.frontend.K1only
@Rule(key = "S6530")
class ReasonableTypeCastsCheck : AbstractCheck() {
override fun visitKtFile(file: KtFile, context: KotlinFileContext) {
context.diagnostics
context.kaDiagnostics
.mapNotNull { diagnostic ->
when (diagnostic.factory) {
Errors.UNCHECKED_CAST -> "Remove this unchecked cast."
Errors.CAST_NEVER_SUCCEEDS -> "Remove this cast that can never succeed."
when (diagnostic.factoryName) {
FirErrors.UNCHECKED_CAST.name -> "Remove this unchecked cast."
FirErrors.CAST_NEVER_SUCCEEDS.name -> "Remove this cast that can never succeed."
else -> null
}?.let { diagnostic to it }
}.forEach { (diagnostic, msg) ->
context.reportIssue(diagnostic.psiElement, msg)
context.reportIssue(diagnostic.psi, msg)
}
}
}

0 comments on commit 4d96420

Please sign in to comment.