statement has no effect inspection

This commit is contained in:
alexey.ivanov
2010-03-10 18:46:35 +03:00
parent f8f775fabf
commit c8c1da4d25
5 changed files with 98 additions and 1 deletions
@@ -184,6 +184,9 @@ INSP.NAME.raising.new.style.class=Raising a new style class
# PyDocstringInspection
INSP.NAME.docstring=Missing or empty docstring
# PyStatementEffectInspection
INSP.NAME.statement.effect=Statement has no effect
# Refactoring
# introduce
refactoring.introduce.name.error=Incorrect name
@@ -0,0 +1,70 @@
package com.jetbrains.python.inspections;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
/**
* Created by IntelliJ IDEA.
* Author: Alexey.Ivanov
* Date: 10.03.2010
* Time: 17:35:16
*/
public class PyStatementEffectInspection extends LocalInspectionTool {
@Nls
@NotNull
@Override
public String getGroupDisplayName() {
return PyBundle.message("INSP.GROUP.python");
}
@Nls
@NotNull
@Override
public String getDisplayName() {
return PyBundle.message("INSP.NAME.statement.effect");
}
@NotNull
@Override
public String getShortName() {
return "PyStatementEffectInspection";
}
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new Visitor(holder);
}
private static class Visitor extends PyInspectionVisitor {
public Visitor(final ProblemsHolder holder) {
super(holder);
}
@Override
public void visitPyExpressionStatement(PyExpressionStatement node) {
PyExpression expression = node.getExpression();
if (expression instanceof PyCallExpression || expression instanceof PyYieldExpression) {
return;
}
PyTryPart tryPart = PsiTreeUtil.getParentOfType(node, PyTryPart.class);
if (tryPart != null) {
final PyStatementList statementList = tryPart.getStatementList();
if (statementList == null) {
return;
}
if (statementList.getStatements().length == 1 && statementList.getStatements()[0] == node) {
return;
}
}
registerProblem(expression, "Statement seems to have no effect");
}
}
}
@@ -29,8 +29,9 @@ public class PythonInspectionToolProvider implements InspectionToolProvider {
PyExceptionInheritInspection.class,
PyDefaultArgumentInspection.class,
PyRaisingNewStyleClassInspection.class,
PyDocstringInspection.class,
PyUnboundLocalVariableInspection.class,
PyDocstringInspection.class
PyStatementEffectInspection.class
};
}
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>test.py</file>
<line>1</line>
<description>Statement seems to have no effect</description>
</problem>
<problem>
<file>test.py</file>
<line>2</line>
<description>Statement seems to have no effect</description>
</problem>
<problem>
<file>test.py</file>
<line>5</line>
<description>Statement seems to have no effect</description>
</problem>
</problems>
@@ -142,4 +142,9 @@ public class PythonInspectionsTest extends PyLightFixtureTestCase {
LocalInspectionTool inspection = new PyDocstringInspection();
doTest(getTestName(false), inspection);
}
public void testPyStatementEffectInspection() throws Throwable {
LocalInspectionTool inspection = new PyStatementEffectInspection();
doTest(getTestName(false), inspection);
}
}