option to ignore super qualifiers if they help to avoid ambiguity

though no ambiguity is there by jls, users may prefer to specify qualifier to help understand the code (IDEA-159680)
This commit is contained in:
Anna Kozlova
2018-01-05 20:05:50 +01:00
parent 442e7f64e2
commit b11d12a7df
4 changed files with 60 additions and 5 deletions
@@ -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);
}
@@ -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.
<!-- tooltip end -->
<p>
Use the checkbox below to ignore qualifiers, which help to distinguish super class field access from the same named field of the outer class.
<p>
</body>
</html>
@@ -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);
}
}
}
@@ -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