implemented pylint E1111

Inspection is used when an assignment is done on a function call but the inferred function doesn't return anything.
This commit is contained in:
Ekaterina Tuzova
2013-03-22 14:43:49 +04:00
parent eafde14771
commit d74c7f2ab1
9 changed files with 126 additions and 0 deletions
@@ -318,6 +318,7 @@
<localInspection language="Python" shortName="PyPep8Inspection" displayName="PEP 8 coding style violation" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyPep8Inspection"/>
<localInspection language="Python" shortName="PyAttributeOutsideInitInspection" displayName="Instance attribute defined outside _init_" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyAttributeOutsideInitInspection"/>
<localInspection language="Python" shortName="PyClassHasNoInitInspection" displayName="Class has no __init__ method" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyClassHasNoInitInspection"/>
<localInspection language="Python" shortName="PyNoneFunctionAssignmentInspection" displayName="Assigning function call that doesn't return anything" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyNoneFunctionAssignmentInspection"/>
<localInspection language="Python" shortName="PyProtectedMemberInspection" displayName="Access to a protected member of a class" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyProtectedMemberInspection"/>
<localInspection language="Python" shortName="PyDocstringTypesInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.docstring.types" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyDocstringTypesInspection"/>
<localInspection language="Python" shortName="PyShadowingBuiltinsInspection" displayName="Shadowing built-ins" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyShadowingBuiltinsInspection"/>
@@ -476,6 +476,9 @@ INSP.NAME.class.has.no.init=Class has no __init__ method
INSP.class.has.no.init=Class has no __init__ method
INSP.parent.$0.has.no.init=Parent {0} has no __init__ method
#PyNoneFunctionAssignmentInspection
INSP.NAME.none.function.assignment=Assigning function call that doesn't return anything (None)
INSP.none.function.assignment=Function {0} doesn''t return anything
# Refactoring
# introduce
@@ -0,0 +1,57 @@
package com.jetbrains.python.inspections;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElementVisitor;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyAssignmentStatement;
import com.jetbrains.python.psi.PyCallExpression;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.types.PyNoneType;
import com.jetbrains.python.psi.types.PyType;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: ktisha
*
* pylint E1111
*
* Used when an assignment is done on a function call but the inferred function doesn't return anything.
*/
public class PyNoneFunctionAssignmentInspection extends PyInspection {
@Nls
@NotNull
@Override
public String getDisplayName() {
return PyBundle.message("INSP.NAME.none.function.assignment");
}
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder,
boolean isOnTheFly,
@NotNull LocalInspectionToolSession session) {
return new Visitor(holder, session);
}
private static class Visitor extends PyInspectionVisitor {
public Visitor(@Nullable ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
super(holder, session);
}
@Override
public void visitPyAssignmentStatement(PyAssignmentStatement node) {
final PyExpression value = node.getAssignedValue();
if (value instanceof PyCallExpression) {
final PyType type = myTypeEvalContext.getType(value);
final PyExpression callee = ((PyCallExpression)value).getCallee();
if (type instanceof PyNoneType && callee != null) {
registerProblem(node, PyBundle.message("INSP.none.function.assignment", callee.getName()));
}
}
}
}
}
@@ -0,0 +1,6 @@
__author__ = 'ktisha'
def foo():
a = 1
<weak_warning descr="Function foo doesn't return anything">b = foo()</weak_warning>
@@ -0,0 +1,6 @@
__author__ = 'ktisha'
def foo(x):
return x
b = foo()
@@ -0,0 +1,6 @@
__author__ = 'ktisha'
def foo():
pass
<weak_warning descr="Function foo doesn't return anything">b = foo()</weak_warning>
@@ -0,0 +1,6 @@
__author__ = 'ktisha'
def foo():
return None
<weak_warning descr="Function foo doesn't return anything">b = foo()</weak_warning>
@@ -0,0 +1,6 @@
__author__ = 'ktisha'
def foo():
return 3
b = foo()
@@ -0,0 +1,35 @@
package com.jetbrains.python.inspections;
import com.jetbrains.python.fixtures.PyTestCase;
/**
* User: ktisha
*/
public class PyNoneFunctionAssignmentInspectionTest extends PyTestCase {
public void testPass() {
doTest();
}
public void testReturnNone() {
doTest();
}
public void testNoReturn() {
doTest();
}
public void testTrueNegative() {
doTest();
}
public void testNoType() {
doTest();
}
private void doTest() {
myFixture.configureByFile("inspections/PyNoneFunctionAssignmentInspection/" + getTestName(true) + ".py");
myFixture.enableInspections(PyNoneFunctionAssignmentInspection.class);
myFixture.checkHighlighting(false, false, true);
}
}