diff --git a/java/java-tests/testData/inspection/redundantSuppress/ignoreWithAnnotation/expected.xml b/java/java-tests/testData/inspection/redundantSuppress/ignoreWithAnnotation/expected.xml new file mode 100644 index 000000000000..5e933496b9cf --- /dev/null +++ b/java/java-tests/testData/inspection/redundantSuppress/ignoreWithAnnotation/expected.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantSuppress/ignoreWithAnnotation/src/x/X.java b/java/java-tests/testData/inspection/redundantSuppress/ignoreWithAnnotation/src/x/X.java new file mode 100644 index 000000000000..26e14700b61c --- /dev/null +++ b/java/java-tests/testData/inspection/redundantSuppress/ignoreWithAnnotation/src/x/X.java @@ -0,0 +1,9 @@ +package x; + +class X { + public String foo(String foo) { + @SuppressWarnings("UnnecessaryLocalVariable") + String bar = foo; + return new String(bar); + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java index 16ea8e577998..f95a66380153 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java @@ -27,6 +27,7 @@ import com.intellij.codeInspection.javaDoc.JavaDocReferenceInspection; import com.intellij.psi.PsiElement; import com.intellij.psi.impl.source.tree.injected.MyTestInjector; import com.intellij.testFramework.InspectionTestCase; +import com.siyeh.ig.dataflow.UnnecessaryLocalVariableInspection; import com.siyeh.ig.migration.RawUseOfParameterizedTypeInspection; import org.jetbrains.annotations.NotNull; @@ -41,6 +42,7 @@ public class RedundantSuppressTest extends InspectionTestCase { new LocalInspectionToolWrapper(new JavaDocReferenceInspection()), new LocalInspectionToolWrapper(new I18nInspection()), new LocalInspectionToolWrapper(new RawUseOfParameterizedTypeInspection()), + new LocalInspectionToolWrapper(new UnnecessaryLocalVariableInspection()), new GlobalInspectionToolWrapper(new EmptyMethodInspection()), new GlobalInspectionToolWrapper(new UnusedDeclarationInspection())}; @@ -76,6 +78,8 @@ public class RedundantSuppressTest extends InspectionTestCase { doTest(); } + public void testIgnoreWithAnnotation() { doTest(); } + public void testSuppressAll() { try { ((RedundantSuppressInspection)myWrapper.getTool()).IGNORE_ALL = true; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspectionBase.java index 1bb0cf934b08..c191f0b44f25 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspectionBase.java @@ -16,6 +16,7 @@ package com.siyeh.ig.dataflow; import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil; +import com.intellij.codeInspection.JavaSuppressionUtil; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.WriteExternalException; @@ -98,8 +99,16 @@ public class UnnecessaryLocalVariableInspectionBase extends BaseInspection { super.visitLocalVariable(variable); if (m_ignoreAnnotatedVariablesNew) { final PsiModifierList list = variable.getModifierList(); - if (list != null && list.getAnnotations().length > 0) { - return; + if (list != null) { + int length = list.getAnnotations().length; + if (length > 0) { + PsiAnnotation annotation = list.findAnnotation(SuppressWarnings.class.getName()); + if (annotation == null || + !JavaSuppressionUtil.getInspectionIdsSuppressedInAnnotation(list) + .contains(UnnecessaryLocalVariableInspectionBase.this.getSuppressId())) { + return; + } + } } } if (isCopyVariable(variable)) {