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)
This commit is contained in:
Yan Zhulanow
2016-05-17 20:39:53 +03:00
parent 8caee7940e
commit d1447faf2b
3 changed files with 7 additions and 2 deletions
@@ -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 -> {
@@ -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)
}
}
+4
View File
@@ -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.<error descr="Call requires API level 16 (current min is 1): `setBackground`">setBackground(null)</error>
// Ok
Bundle().getInt("")
// Virtual call
<error descr="Call requires API level 11 (current min is 1): `getActionBar`">getActionBar()</error> // API 11
<error descr="Call requires API level 11 (current min is 1): `getActionBar`">actionBar</error> // API 11