Android Lint: Use Uast to check for SuppressLint annotation
#KT-14800 Fixed Target versions 1.1.5
This commit is contained in:
+1
-1
@@ -73,7 +73,7 @@ class JavaPerformanceTest(context: Context, attrs: AttributeSet, defStyle: Int)
|
|||||||
|
|
||||||
// This one should not be reported:
|
// This one should not be reported:
|
||||||
@SuppressLint("UseSparseArrays")
|
@SuppressLint("UseSparseArrays")
|
||||||
val myOtherMap = <warning descr="Use `new SparseArray<Object>(...)` instead for better performance">HashMap<Int, Any>()</warning>
|
val myOtherMap = HashMap<Int, Any>()
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int,
|
protected fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int,
|
||||||
|
|||||||
Vendored
+1
-1
@@ -61,7 +61,7 @@ class ToastTest(context: Context) : Activity() {
|
|||||||
|
|
||||||
private fun checkSuppress2(context: Context) {
|
private fun checkSuppress2(context: Context) {
|
||||||
@android.annotation.SuppressLint("ShowToast")
|
@android.annotation.SuppressLint("ShowToast")
|
||||||
val toast = Toast.<warning descr="Toast created but not shown: did you forget to call `show()` ?">makeText</warning>(this, "MyToast", Toast.LENGTH_LONG)
|
val toast = Toast.makeText(this, "MyToast", Toast.LENGTH_LONG)
|
||||||
}
|
}
|
||||||
|
|
||||||
class R {
|
class R {
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ import com.intellij.psi.PsiModifierList;
|
|||||||
import com.intellij.psi.PsiModifierListOwner;
|
import com.intellij.psi.PsiModifierListOwner;
|
||||||
import com.intellij.psi.PsiNameValuePair;
|
import com.intellij.psi.PsiNameValuePair;
|
||||||
|
|
||||||
import org.jetbrains.uast.UElement;
|
import org.jetbrains.uast.*;
|
||||||
import org.jetbrains.org.objectweb.asm.ClassReader;
|
import org.jetbrains.org.objectweb.asm.ClassReader;
|
||||||
import org.jetbrains.org.objectweb.asm.Opcodes;
|
import org.jetbrains.org.objectweb.asm.Opcodes;
|
||||||
import org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode;
|
import org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode;
|
||||||
@@ -2609,9 +2609,8 @@ public class LintDriver {
|
|||||||
boolean checkComments = mClient.checkForSuppressComments() &&
|
boolean checkComments = mClient.checkForSuppressComments() &&
|
||||||
context != null && context.containsCommentSuppress();
|
context != null && context.containsCommentSuppress();
|
||||||
while (scope != null) {
|
while (scope != null) {
|
||||||
if (scope instanceof PsiModifierListOwner) {
|
if (scope instanceof UAnnotated) {
|
||||||
PsiModifierListOwner owner = (PsiModifierListOwner) scope;
|
if (isSuppressed(issue, (UAnnotated) scope)) {
|
||||||
if (isSuppressed(issue, owner.getModifierList())) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2621,7 +2620,7 @@ public class LintDriver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
scope = scope.getUastParent();
|
scope = scope.getUastParent();
|
||||||
if (scope instanceof PsiFile) {
|
if (scope instanceof UFile) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2822,6 +2821,38 @@ public class LintDriver {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the given AST modifier has a suppress annotation for the
|
||||||
|
* given issue (which can be null to check for the "all" annotation)
|
||||||
|
*
|
||||||
|
* @param issue the issue to be checked
|
||||||
|
* @param element the element to check
|
||||||
|
* @return true if the issue or all issues should be suppressed for this
|
||||||
|
* element
|
||||||
|
*/
|
||||||
|
public static boolean isSuppressed(@NonNull Issue issue,
|
||||||
|
@Nullable UAnnotated element) {
|
||||||
|
if (element == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (UAnnotation annotation : element.getAnnotations()) {
|
||||||
|
String fqcn = annotation.getQualifiedName();
|
||||||
|
if (fqcn != null && (fqcn.equals(FQCN_SUPPRESS_LINT)
|
||||||
|
|| fqcn.equals(SUPPRESS_WARNINGS_FQCN)
|
||||||
|
|| fqcn.equals(SUPPRESS_LINT))) { // when missing imports
|
||||||
|
for (UNamedExpression attribute : annotation.getAttributeValues()) {
|
||||||
|
Object value = attribute.getExpression().evaluate();
|
||||||
|
if (value instanceof String && isSuppressed(issue, (String) value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private File mCachedFolder = null;
|
private File mCachedFolder = null;
|
||||||
private int mCachedFolderVersion = -1;
|
private int mCachedFolderVersion = -1;
|
||||||
/** Pattern for version qualifiers */
|
/** Pattern for version qualifiers */
|
||||||
|
|||||||
Reference in New Issue
Block a user