Re-introduced redeclaration inspection

This commit is contained in:
Andrey Vlasovskikh
2013-09-03 18:04:12 +04:00
parent 46bebeb151
commit d55e3c08be
5 changed files with 84 additions and 59 deletions
@@ -5,6 +5,5 @@ This inspection detects redeclarations of names without being used in between, l
x = 2
</pre>
It applies to function and class declarations, and assignments.<br/>
<i>Currently it works in a very limited way and is off by default.</i>
</span></body>
</html>
+1 -1
View File
@@ -260,7 +260,7 @@
<stubElementTypeHolder class="com.jetbrains.python.PyElementTypes"/>
<localInspection language="Python" shortName="PyArgumentListInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.incorrect.call.arguments" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyArgumentListInspection"/>
<localInspection language="Python" shortName="PyRedeclarationInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.redeclaration" groupKey="INSP.GROUP.python" enabledByDefault="false" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyRedeclarationInspection"/>
<localInspection language="Python" shortName="PyRedeclarationInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.redeclaration" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyRedeclarationInspection"/>
<localInspection language="Python" shortName="PyUnresolvedReferencesInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.unresolved.refs" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyUnresolvedReferencesInspection"/>
<localInspection language="Python" shortName="PyInterpreterInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.invalid.interpreter" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyInterpreterInspection"/>
<localInspection language="Python" shortName="PyMethodParametersInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.problematic.first.parameter" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyMethodParametersInspection"/>
@@ -280,7 +280,7 @@ INSP.decorator.receives.unexpected.builtin=This decorator will not receive a cal
# PyRedeclarationInspection
INSP.NAME.redeclaration=Names redeclared without usage
INSP.shadows.same.named.$0.above=Shadows same-named {0} above
INSP.shadows.same.named.$0.above=Shadows a {0} with the same name defined above
# PyUnresolvedReferencesInspection
INSP.NAME.unresolved.refs=Unresolved references
@@ -1,27 +1,30 @@
package com.jetbrains.python.inspections;
import com.intellij.codeInsight.controlflow.ControlFlowUtil;
import com.intellij.codeInsight.controlflow.Instruction;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.util.Function;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyElement;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.PyTargetExpression;
import com.jetbrains.python.psi.resolve.PyResolveUtil;
import com.jetbrains.python.psi.resolve.ResolveProcessor;
import com.jetbrains.python.codeInsight.controlflow.ControlFlowCache;
import com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Annotates declarations that unconditionally ovverride other without these being used.
* E.g.: <code>x = 1; x = 2</code>
* User: dcheryasov
* Date: Nov 14, 2008
* Annotates declarations that unconditionally override others without these being used.
*
* @author dcheryasov
* @author vlan
*
* TODO: Add a rename quick-fix
*/
public class PyRedeclarationInspection extends PyInspection {
@Nls
@@ -30,10 +33,6 @@ public class PyRedeclarationInspection extends PyInspection {
return PyBundle.message("INSP.NAME.redeclaration");
}
@Override
public boolean isEnabledByDefault() {
return false; // too immature yet
}
@NotNull
@Override
@@ -43,54 +42,83 @@ public class PyRedeclarationInspection extends PyInspection {
return new Visitor(holder, session);
}
public static class Visitor extends PyInspectionVisitor {
private static class Visitor extends PyInspectionVisitor {
public Visitor(@Nullable ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
super(holder, session);
}
// TODO: This function is a shame; replace with a proper interface.
private static String _getKind(PsiElement elt) {
if (elt instanceof PyFunction) return PyBundle.message("GNAME.function");
if (elt instanceof PyClass) return PyBundle.message("GNAME.class");
if (elt instanceof PyTargetExpression) return PyBundle.message("GNAME.var");
return PyBundle.message("GNAME.item");
}
private void _checkAbove(PyElement node) {
String name = node.getName();
if (name != null) {
ResolveProcessor proc = new ResolveProcessor(node.getName());
PyResolveUtil.treeCrawlUp(proc, node);
PsiElement found = proc.getResult();
// TODO: check if the redefined name is used somehow
if (found != null && ! (found instanceof PyTargetExpression)) {
final ASTNode identifier = node.getNode().findChildByType(PyTokenTypes.IDENTIFIER);
if (identifier != null) {
registerProblem(
identifier.getPsi(),
PyBundle.message("INSP.shadows.same.named.$0.above", _getKind(found))
);
}
//registerProblem(prev.getNode().findChildByType(PyTokenTypes.IDENTIFIER).getPsi(), "Overridden by same-named " + kind + " below");
}
}
}
// TODO: add a rename quickfix
@Override
public void visitPyFunction(final PyFunction node) {
_checkAbove(node);
if (!isDecorated(node)) {
processElement(node);
}
}
@Override
public void visitPyTargetExpression(final PyTargetExpression node) {
_checkAbove(node);
final ScopeOwner owner = ScopeUtil.getScopeOwner(node);
if (owner instanceof PyFile || owner instanceof PyClass) {
processElement(node);
}
}
@Override
public void visitPyClass(final PyClass node) {
_checkAbove(node);
if (!isDecorated(node)) {
processElement(node);
}
}
private static boolean isDecorated(@NotNull PyDecoratable node) {
boolean isDecorated = false;
final PyDecoratorList decoratorList = node.getDecoratorList();
if (decoratorList != null) {
final PyDecorator[] decorators = decoratorList.getDecorators();
if (decorators.length > 0) {
isDecorated = true;
}
}
return isDecorated;
}
private void processElement(@NotNull final PsiNameIdentifierOwner element) {
final String name = element.getName();
final ScopeOwner owner = ScopeUtil.getScopeOwner(element);
if (owner != null && name != null) {
final Instruction[] instructions = ControlFlowCache.getControlFlow(owner).getInstructions();
final int startInstruction = ControlFlowUtil.findInstructionNumberByElement(instructions, element);
ControlFlowUtil.iteratePrev(startInstruction, instructions, new Function<Instruction, ControlFlowUtil.Operation>() {
@Override
public ControlFlowUtil.Operation fun(Instruction instruction) {
if (instruction instanceof ReadWriteInstruction && instruction.num() != startInstruction) {
final ReadWriteInstruction rwInstruction = (ReadWriteInstruction)instruction;
if (name.equals(rwInstruction.getName())) {
if (rwInstruction.getAccess().isWriteAccess()) {
final PsiElement shadowed = rwInstruction.getElement();
final PsiElement identifier = element.getNameIdentifier();
registerProblem(identifier != null ? identifier : element,
PyBundle.message("INSP.shadows.same.named.$0.above", getKind(shadowed)));
}
return ControlFlowUtil.Operation.BREAK;
}
}
return ControlFlowUtil.Operation.NEXT;
}
});
}
}
}
@NotNull
private static String getKind(@Nullable PsiElement element) {
if (element instanceof PyFunction) {
return PyBundle.message("GNAME.function");
}
else if (element instanceof PyClass) {
return PyBundle.message("GNAME.class");
}
else {
return PyBundle.message("GNAME.var");
}
}
}
@@ -3,23 +3,21 @@
<problem>
<file>dupes.py</file>
<line>2</line>
<description>Shadows same-named class above</description>
<description>Shadows a class with the same name defined above</description>
</problem>
<problem>
<file>dupes.py</file>
<line>5</line>
<description>Shadows same-named function above</description>
<description>Shadows a function with the same name defined above</description>
</problem>
<problem>
<file>dupes.py</file>
<line>8</line>
<description>Shadows same-named function above</description>
<description>Shadows a function with the same name defined above</description>
</problem>
<!--
<problem>
<file>dupes.py</file>
<line>9</line>
<description>Shadows same-named variable above</description>
<description>Shadows a variable with the same name defined above</description>
</problem>
-->
</problems>