java global inspections: fix tag noinspection ignorance (IDEA-203424, IDEA-203421)

This commit is contained in:
Dmitry Batkovich
2018-12-04 14:44:01 +03:00
parent ead2a26bee
commit ea2326f453
4 changed files with 58 additions and 27 deletions
@@ -466,6 +466,7 @@ public class RefJavaManagerImpl extends RefJavaManager {
@Override
public boolean visitDeclaration(@NotNull UDeclaration node) {
processComments(node);
RefElement decl = myRefManager.getReference(node.getSourcePsi());
if (decl != null) {
((RefElementImpl)decl).buildReferences();
@@ -483,33 +484,6 @@ public class RefJavaManagerImpl extends RefJavaManager {
return true;
}
@Override
public boolean visitElement(@NotNull UElement node) {
if (node instanceof UComment) {
PsiElement psi = node.getSourcePsi();
if (psi instanceof PsiDocComment) {
//TODO support suppressions in kotlin
final PsiDocTag[] tags = ((PsiDocComment)psi).getTags();
for (PsiDocTag tag : tags) {
if (Comparing.strEqual(tag.getName(), SuppressionUtilCore.SUPPRESS_INSPECTIONS_TAG_NAME)) {
final PsiElement[] dataElements = tag.getDataElements();
if (dataElements.length > 0) {
final PsiModifierListOwner listOwner = PsiTreeUtil.getParentOfType(psi, PsiModifierListOwner.class);
if (listOwner != null) {
final WritableRefElement element = (WritableRefElement)myRefManager.getReference(listOwner);
if (element != null) {
String suppression = StringUtil.join(dataElements, PsiElement::getText, ",");
element.addSuppression(suppression);
}
}
}
}
}
}
}
return super.visitElement(node);
}
@Override
public boolean visitField(@NotNull UField node) {
visitDeclaration(node);
@@ -588,6 +562,33 @@ public class RefJavaManagerImpl extends RefJavaManager {
}
}
//TODO support suppressions by comment tag in kotlin
private void processComments(@NotNull UElement node) {
for (UComment comment : node.getComments()) {
if (comment instanceof UComment) {
PsiElement psi = comment.getSourcePsi();
if (psi instanceof PsiDocComment) {
final PsiDocTag[] tags = ((PsiDocComment)psi).getTags();
for (PsiDocTag tag : tags) {
if (Comparing.strEqual(tag.getName(), SuppressionUtilCore.SUPPRESS_INSPECTIONS_TAG_NAME)) {
final PsiElement[] dataElements = tag.getDataElements();
if (dataElements.length > 0) {
final PsiModifierListOwner listOwner = PsiTreeUtil.getParentOfType(psi, PsiModifierListOwner.class);
if (listOwner != null) {
final WritableRefElement element = (WritableRefElement)myRefManager.getReference(listOwner);
if (element != null) {
String suppression = StringUtil.join(dataElements, PsiElement::getText, ",");
element.addSuppression(suppression);
}
}
}
}
}
}
}
}
}
private void addSuppressionsForSiblings(PsiField listOwner, String suppressId) {
PsiField field = listOwner;
while (true) {
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>A.java</file>
<line>16</line>
<problem_class>unused declaration</problem_class>
<description>Field has no usages.</description>
</problem>
</problems>
@@ -0,0 +1,17 @@
/**
* @noinspection bla-bla, unused, bla-bla
*/
class A {
String a;
}
/**
* @noinspection unused
*/
class B {
String b;
}
class C {
String c;
}
@@ -93,6 +93,10 @@ public class UnusedDeclarationTest extends AbstractUnusedDeclarationTest {
doTest();
}
public void testSuppressByNoinspectionTag() {
doTest();
}
public void testReachableFromXml() {
doTest();
}