redundant suppression: don't delete all same comments/top annotation when one suppress comment is actually to be deleted

This commit is contained in:
Anna.Kozlova
2018-10-04 18:20:21 +02:00
parent ecee57369f
commit bc61fc52a0
5 changed files with 72 additions and 23 deletions
@@ -64,29 +64,37 @@ public class RemoveSuppressWarningAction implements LocalQuickFix {
PsiElement element = descriptor.getPsiElement();
try {
if (element != null) {
final PsiModifierListOwner commentOwner = PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class, false);
if (commentOwner != null) {
final PsiElement psiElement = JavaSuppressionUtil.getElementMemberSuppressedIn(commentOwner, myID);
if (psiElement instanceof PsiAnnotation) {
removeFromAnnotation((PsiAnnotation)psiElement);
} else if (psiElement instanceof PsiDocComment) {
removeFromJavaDoc((PsiDocComment)psiElement);
} else { //try to remove from all comments
final Set<PsiComment> comments = new HashSet<>();
commentOwner.accept(new PsiRecursiveElementWalkingVisitor() {
@Override public void visitComment(final PsiComment comment) {
super.visitComment(comment);
if (comment.getText().contains(myID)) {
comments.add(comment);
if (element instanceof PsiComment) {
removeFromComment((PsiComment)element);
}
else {
final PsiModifierListOwner commentOwner = PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class, false);
if (commentOwner != null) {
final PsiElement psiElement = JavaSuppressionUtil.getElementMemberSuppressedIn(commentOwner, myID);
if (psiElement instanceof PsiAnnotation) {
removeFromAnnotation((PsiAnnotation)psiElement);
}
else if (psiElement instanceof PsiDocComment) {
removeFromJavaDoc((PsiDocComment)psiElement);
}
else { //try to remove from all comments
final Set<PsiComment> comments = new HashSet<>();
commentOwner.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitComment(final PsiComment comment) {
super.visitComment(comment);
if (comment.getText().contains(myID)) {
comments.add(comment);
}
}
});
for (PsiComment comment : comments) {
try {
removeFromComment(comment);
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
});
for (PsiComment comment : comments) {
try {
removeFromComment(comment, comments.size() > 1);
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
@@ -104,7 +112,7 @@ public class RemoveSuppressWarningAction implements LocalQuickFix {
return QuickFixBundle.message("remove.suppression.action.name", myID);
}
private void removeFromComment(final PsiComment comment, final boolean checkLine) throws IncorrectOperationException {
private void removeFromComment(final PsiComment comment) throws IncorrectOperationException {
String newText = removeFromElementText(comment);
if (newText != null) {
if (newText.isEmpty()) {
@@ -9,6 +9,9 @@ public class Test {
void foo() {
List<ArrayList<String>> list = foo(new ArrayList<String>());
//noinspection unchecked
ArrayList<String> list = new ArrayList();
}
}
@@ -0,0 +1,17 @@
// "Remove 'unchecked' suppression" "true"
import java.util.*;
public class Test {
@SafeVarargs
static <T> List<T> foo(T... t){
return null;
}
@SuppressWarnings("unchecked")
void foo() {
List<ArrayList<String>> list = foo(new ArrayList<String>());
ArrayList<String> list = new ArrayList();
}
}
@@ -10,6 +10,9 @@ public class Test {
void foo() {
//noinspection unche<caret>cked
List<ArrayList<String>> list = foo(new ArrayList<String>());
//noinspection unchecked
ArrayList<String> list = new ArrayList();
}
}
@@ -0,0 +1,18 @@
// "Remove 'unchecked' suppression" "true"
import java.util.*;
public class Test {
@SafeVarargs
static <T> List<T> foo(T... t){
return null;
}
@SuppressWarnings("unchecked")
void foo() {
//noinspection unche<caret>cked
List<ArrayList<String>> list = foo(new ArrayList<String>());
ArrayList<String> list = new ArrayList();
}
}