diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessarySuperQualifierInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessarySuperQualifierInspection.java index 33164b961540..56854bb4dc1d 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessarySuperQualifierInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessarySuperQualifierInspection.java @@ -18,6 +18,7 @@ package com.siyeh.ig.style; import com.intellij.codeInspection.CleanupLocalInspectionTool; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; @@ -25,12 +26,16 @@ import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.psiutils.ClassUtils; import com.siyeh.ig.psiutils.MethodUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import javax.swing.*; + public class UnnecessarySuperQualifierInspection extends BaseInspection implements CleanupLocalInspectionTool { + public boolean ignoreClarification = false; @Override @Nls @@ -48,6 +53,12 @@ public class UnnecessarySuperQualifierInspection extends BaseInspection implemen ); } + @Nullable + @Override + public JComponent createOptionsPanel() { + return new SingleCheckboxOptionsPanel("Ignore clarification 'super' qualifier", this, "ignoreClarification"); + } + @Override @Nullable protected InspectionGadgetsFix buildFix(Object... infos) { @@ -71,11 +82,15 @@ public class UnnecessarySuperQualifierInspection extends BaseInspection implemen @Override public BaseInspectionVisitor buildVisitor() { - return new UnnecessarySuperQualifierVisitor(); + return new UnnecessarySuperQualifierVisitor(ignoreClarification); } - private static class UnnecessarySuperQualifierVisitor - extends BaseInspectionVisitor { + private static class UnnecessarySuperQualifierVisitor extends BaseInspectionVisitor { + private boolean myIgnoreClarification; + + public UnnecessarySuperQualifierVisitor(boolean ignoreClarification) { + myIgnoreClarification = ignoreClarification; + } @Override public void visitSuperExpression(PsiSuperExpression expression) { @@ -91,8 +106,7 @@ public class UnnecessarySuperQualifierInspection extends BaseInspection implemen final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)parent; final PsiElement grandParent = referenceExpression.getParent(); if (grandParent instanceof PsiMethodCallExpression) { - final PsiMethodCallExpression methodCallExpression = - (PsiMethodCallExpression)grandParent; + final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent; if (!hasUnnecessarySuperQualifier(methodCallExpression)) { return; } @@ -101,6 +115,20 @@ public class UnnecessarySuperQualifierInspection extends BaseInspection implemen if (!hasUnnecessarySuperQualifier(referenceExpression)) { return; } + if (myIgnoreClarification) { + PsiClass containingClass = ClassUtils.getContainingClass(expression); + if (containingClass != null) { + final PsiElement classParent = containingClass.getParent(); + final String referenceText = referenceExpression.getReferenceName(); + if (referenceText != null) { + PsiVariable variable = PsiResolveHelper.SERVICE.getInstance(expression.getProject()) + .resolveAccessibleReferencedVariable(referenceText, classParent); + if (variable != null && variable != referenceExpression.resolve()) { + return; + } + } + } + } } registerError(expression, ProblemHighlightType.LIKE_UNUSED_SYMBOL); } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessarySuperQualifier.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessarySuperQualifier.html index 5c05f972348d..8af425618d47 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessarySuperQualifier.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessarySuperQualifier.html @@ -5,6 +5,9 @@ qualifier in method calls and fields references. A super qualifier is unnecessar when the field or method of the super class is not overridden in the calling class.

+Use the checkbox below to ignore qualifiers, which help to distinguish super class field access from the same named field of the outer class. +

+ \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_super_qualifier/IgnoreClarificationSuperQualifier.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_super_qualifier/IgnoreClarificationSuperQualifier.java new file mode 100644 index 000000000000..f8caf61fb043 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_super_qualifier/IgnoreClarificationSuperQualifier.java @@ -0,0 +1,17 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ +package com.siyeh.igtest.style.unnecessary_super_qualifier; + +public class IgnoreClarificationSuperQualifier { + int f; + class Base { + int f; + } + + class Child extends Base { + { + System.out.println(super.f); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessarySuperQualifierInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessarySuperQualifierInspectionTest.java index 5403dbdca6e0..f075fa12aabd 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessarySuperQualifierInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessarySuperQualifierInspectionTest.java @@ -27,6 +27,13 @@ public class UnnecessarySuperQualifierInspectionTest extends LightInspectionTest public void testUnnecessarySuperQualifier() { doTest(); } + + public void testIgnoreClarificationSuperQualifier() { + UnnecessarySuperQualifierInspection inspection = (UnnecessarySuperQualifierInspection)getInspection(); + inspection.ignoreClarification = true; + myFixture.enableInspections(inspection); + doTest(); + } @Nullable @Override