From d1447faf2bfff73b10d6f0f39687167b44f29adb Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Tue, 17 May 2016 20:39:53 +0300 Subject: [PATCH] Fix #KT-12015 (Kotlin Lint False Positive for Bundle.getInt()). Android firstly checks API version against the function call receiver type, so we should do the same to be consistent. (cherry picked from commit c3d01ec) (cherry picked from commit 5c78acc) --- .../src/com/android/tools/klint/checks/ApiDetector.kt | 3 ++- .../jetbrains/kotlin/uast/expressions/KotlinUExpression.kt | 2 +- plugins/uast-kotlin/testData/lint/apiCheck.kt | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/lint/lint-checks/src/com/android/tools/klint/checks/ApiDetector.kt b/plugins/lint/lint-checks/src/com/android/tools/klint/checks/ApiDetector.kt index d3d398aa8f8..3f550ebe243 100755 --- a/plugins/lint/lint-checks/src/com/android/tools/klint/checks/ApiDetector.kt +++ b/plugins/lint/lint-checks/src/com/android/tools/klint/checks/ApiDetector.kt @@ -166,7 +166,8 @@ open class ApiDetector : Detector(), UastScanner { val parentClass = declaration.parent as? UClass ?: return if (!isSdkClass(parentClass) || checkAosp(parentClass)) return - val parentInternalName = parentClass.internalName ?: return + val parentInternalName = (node as? UCallExpression)?.getReceiver()?.getExpressionType()?.resolveClass(context)?.internalName + ?: parentClass.internalName ?: return when (declaration) { is UFunction -> { diff --git a/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/expressions/KotlinUExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/expressions/KotlinUExpression.kt index b25cb5219ee..bc025ee1df4 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/expressions/KotlinUExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/expressions/KotlinUExpression.kt @@ -28,7 +28,7 @@ import org.jetbrains.uast.psi.PsiElementBacked interface KotlinUElementWithType : UExpression, PsiElementBacked { override fun getExpressionType(): UType? { val ktElement = psi as? KtExpression ?: return null - val ktType = ktElement.analyze(BodyResolveMode.PARTIAL)[BindingContext.EXPECTED_EXPRESSION_TYPE, ktElement] ?: return null + val ktType = ktElement.analyze(BodyResolveMode.PARTIAL)[BindingContext.EXPRESSION_TYPE_INFO, ktElement]?.type ?: return null return KotlinConverter.convert(ktType, ktElement.project, null) } } diff --git a/plugins/uast-kotlin/testData/lint/apiCheck.kt b/plugins/uast-kotlin/testData/lint/apiCheck.kt index ef19427839e..6ddd00f8cde 100644 --- a/plugins/uast-kotlin/testData/lint/apiCheck.kt +++ b/plugins/uast-kotlin/testData/lint/apiCheck.kt @@ -23,6 +23,7 @@ import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION_CODES import android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH import android.os.Build.VERSION_CODES.JELLY_BEAN +import android.os.Bundle import android.widget.TextView @Suppress("SENSELESS_COMPARISON", "UNUSED_EXPRESSION", "UsePropertyAccessSyntax", "UNUSED_VARIABLE", "unused", "UNUSED_PARAMETER", "DEPRECATION") @@ -31,6 +32,9 @@ class ApiCallTest: Activity() { fun method(chronometer: Chronometer, locator: DOMLocator) { chronometer.setBackground(null) + // Ok + Bundle().getInt("") + // Virtual call getActionBar() // API 11 actionBar // API 11