From c8c1da4d25b988ac8fa7f5ac4e2bd8c007f83c46 Mon Sep 17 00:00:00 2001 From: "alexey.ivanov" Date: Wed, 10 Mar 2010 18:22:22 +0300 Subject: [PATCH] statement has no effect inspection --- .../com/jetbrains/python/PyBundle.properties | 3 + .../PyStatementEffectInspection.java | 70 +++++++++++++++++++ .../PythonInspectionToolProvider.java | 3 +- .../PyStatementEffectInspection/expected.xml | 18 +++++ .../python/PythonInspectionsTest.java | 5 ++ 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java create mode 100644 python/testData/inspections/PyStatementEffectInspection/expected.xml diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index 886d1eaf255b..34dfc337da02 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -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 diff --git a/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java b/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java new file mode 100644 index 000000000000..8c5650b7c358 --- /dev/null +++ b/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java @@ -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"); + } + } +} diff --git a/python/src/com/jetbrains/python/inspections/PythonInspectionToolProvider.java b/python/src/com/jetbrains/python/inspections/PythonInspectionToolProvider.java index 0a5b87f0a7bc..f0b65150a7b1 100644 --- a/python/src/com/jetbrains/python/inspections/PythonInspectionToolProvider.java +++ b/python/src/com/jetbrains/python/inspections/PythonInspectionToolProvider.java @@ -29,8 +29,9 @@ public class PythonInspectionToolProvider implements InspectionToolProvider { PyExceptionInheritInspection.class, PyDefaultArgumentInspection.class, PyRaisingNewStyleClassInspection.class, + PyDocstringInspection.class, PyUnboundLocalVariableInspection.class, - PyDocstringInspection.class + PyStatementEffectInspection.class }; } } diff --git a/python/testData/inspections/PyStatementEffectInspection/expected.xml b/python/testData/inspections/PyStatementEffectInspection/expected.xml new file mode 100644 index 000000000000..e5502ae4f428 --- /dev/null +++ b/python/testData/inspections/PyStatementEffectInspection/expected.xml @@ -0,0 +1,18 @@ + + + + test.py + 1 + Statement seems to have no effect + + + test.py + 2 + Statement seems to have no effect + + + test.py + 5 + Statement seems to have no effect + + \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java index 645180771006..20f95812773c 100644 --- a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java +++ b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java @@ -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); + } }