Fixed bugs in Kotlin Android Lint CleanupDetector

#KT-14780 Fixed
#KT-14677 Fixed

(cherry picked from commit f591b4958e4a836d37d70c2ad8ff15e626c6342a)
This commit is contained in:
Vyacheslav Gerasimov
2017-01-24 13:27:00 +03:00
parent 68b223211c
commit d0f1b81bfa
7 changed files with 307 additions and 126 deletions
+36
View File
@@ -0,0 +1,36 @@
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintRecycleInspection
@file:Suppress("UNUSED_VARIABLE")
import android.app.Activity
import android.os.Bundle
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val cursor = contentResolver.<warning descr="This `Cursor` should be freed up after use with `#close()`">query</warning>(null, null, null, null, null)
// WARNING
contentResolver.<warning descr="This `Cursor` should be freed up after use with `#close()`">query</warning>(null, null, null, null, null)
// OK, closed in chained call
contentResolver.query(null, null, null, null, null).close()
// KT-14677: Kotlin Lint: "Missing recycle() calls" report cursor with `use()` call
val cursorUsed = contentResolver.query(null, null, null, null, null)
cursorUsed.use { }
// OK, used in chained call
contentResolver.query(null, null, null, null, null).use {
}
// KT-13372: Android Lint for Kotlin: false positive "Cursor should be freed" inside 'if' expression
if (true) {
val c = contentResolver.query(null, null, null, null, null)
c.close()
}
}
}
+10 -2
View File
@@ -1,7 +1,10 @@
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintCommitTransactionInspection
@file:Suppress("UNUSED_VARIABLE")
import android.app.Activity
import android.app.FragmentTransaction
import android.app.FragmentManager
import android.os.Bundle
class MainActivity : Activity() {
@@ -17,8 +20,7 @@ class MainActivity : Activity() {
transaction2.commit()
//WARNING
@Suppress("UNUSED_VARIABLE")
val transaction3 = fragmentManager.<warning>beginTransaction</warning>()
val transaction3 = fragmentManager.<warning descr="This transaction should be completed with a `commit()` call">beginTransaction</warning>()
//OK
fragmentManager.beginTransaction().commit()
@@ -30,4 +32,10 @@ class MainActivity : Activity() {
a.commit()
}
}
// KT-14780: Kotlin Lint: "Missing commit() calls" false positive when the result of `commit()` is assigned or used as receiver
fun testResultOfCommit(fm: FragmentManager) {
val r1 = fm.beginTransaction().hide(fm.findFragmentByTag("aTag")).commit()
val r2 = fm.beginTransaction().hide(fm.findFragmentByTag("aTag")).commit().toString()
}
}
+5
View File
@@ -66,4 +66,9 @@ class SharedPrefsText(context: Context) : Activity() {
val editor = preferences.<warning descr="`SharedPreferences.edit()` without a corresponding `commit()` or `apply()` call"><warning descr="`SharedPreferences.edit()` without a corresponding `commit()` or `apply()` call">edit()</warning></warning>
editor.putString("foo", "bar")
}
fun testResultOfCommit() {
val r1 = PreferenceManager.getDefaultSharedPreferences(this).edit().putString("wat", "wat").commit()
val r2 = PreferenceManager.getDefaultSharedPreferences(this).edit().putString("wat", "wat").commit().toString()
}
}
+23
View File
@@ -0,0 +1,23 @@
// INSPECTION_CLASS: org.jetbrains.android.inspections.klint.AndroidLintInspectionToolProvider$AndroidKLintRecycleInspection
@file:Suppress("UNUSED_VARIABLE")
import android.app.Activity
import android.os.Bundle
import android.view.VelocityTracker
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
VelocityTracker.<warning descr="This `VelocityTracker` should be recycled after use with `#recycle()`">obtain</warning>()
VelocityTracker.obtain().recycle()
val v1 = VelocityTracker.<warning descr="This `VelocityTracker` should be recycled after use with `#recycle()`">obtain</warning>()
val v2 = VelocityTracker.obtain()
v2.recycle()
}
}