@@ -24,11 +24,11 @@ import org.jetbrains.kotlin.psi.KtOperationReferenceExpression
24
24
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
25
25
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
26
26
import org.jetbrains.kotlin.psi.psiUtil.isPrivate
27
- import org.jetbrains.kotlin.resolve.BindingContext
28
27
import org.sonar.check.Rule
29
28
import org.sonarsource.kotlin.api.checks.AbstractCheck
30
29
import org.sonarsource.kotlin.api.checks.isInfix
31
30
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
31
+ import org.sonarsource.kotlin.api.visiting.withKaSession
32
32
33
33
// Serializable method should not raise any issue in Kotlin.
34
34
private val IGNORED_METHODS : Set <String > = setOf (
@@ -46,13 +46,12 @@ private val COMMON_ANNOTATIONS = listOf(
46
46
" kotlin.jvm.Throws" ,
47
47
)
48
48
49
- @org.sonarsource.kotlin.api.frontend.K1only
50
49
@Rule(key = " S1144" )
51
50
class UnusedPrivateMethodCheck : AbstractCheck () {
52
51
53
52
override fun visitClass (klass : KtClass , context : KotlinFileContext ) {
54
53
if (! klass.isTopLevel()) return
55
- klass.collectDescendantsOfType<KtNamedFunction > { it.shouldCheckForUsage(context.bindingContext ) }
54
+ klass.collectDescendantsOfType<KtNamedFunction > { it.shouldCheckForUsage() }
56
55
.forEach {
57
56
val functionName = it.name!!
58
57
if (! IGNORED_METHODS .contains(functionName) && ! it.isReferencedIn(klass, functionName)) {
@@ -71,15 +70,15 @@ class UnusedPrivateMethodCheck : AbstractCheck() {
71
70
private fun KtClass.hasInfixReferences (name : String ) =
72
71
anyDescendantOfType<KtOperationReferenceExpression > { it.getReferencedName() == name }
73
72
74
- private fun KtNamedFunction.shouldCheckForUsage (bindingContext : BindingContext ) =
73
+ private fun KtNamedFunction.shouldCheckForUsage () =
75
74
isPrivate()
76
75
&& ! hasModifier(KtTokens .OPERATOR_KEYWORD )
77
- && (annotationEntries.isEmpty() || annotatedWithCommonAnnotations(bindingContext ))
76
+ && (annotationEntries.isEmpty() || annotatedWithCommonAnnotations())
78
77
79
- private fun KtNamedFunction.annotatedWithCommonAnnotations (bindingContext : BindingContext ) =
80
- bindingContext[ BindingContext . FUNCTION , this ]
81
- ?.annotations
82
- ?.all { it.fqName?.asString() in COMMON_ANNOTATIONS }
83
- ? : false
78
+ private fun KtNamedFunction.annotatedWithCommonAnnotations () = withKaSession {
79
+ symbol.annotations.all {
80
+ it.classId?.asFqNameString() in COMMON_ANNOTATIONS
81
+ }
82
+ }
84
83
85
84
}
0 commit comments