Don't mark parameters of empty methods with no body as unused (PY-7028)

This commit is contained in:
Andrey Vlasovskikh
2012-08-03 19:32:22 +04:00
parent 7c4589b2fb
commit 3c84d4bc13
6 changed files with 77 additions and 10 deletions
@@ -29,9 +29,7 @@ import com.jetbrains.python.psi.search.PyOverridingMethodsSearch;
import com.jetbrains.python.psi.search.PySuperMethodsSearch;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
/**
* @author oleg
@@ -215,7 +213,8 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
final UnusedLocalFilter[] filters = Extensions.getExtensions(UnusedLocalFilter.EP_NAME);
// Register problems
Set<PyFunction> functionsWithInheritors = new HashSet<PyFunction>();
final Set<PyFunction> functionsWithInheritors = new HashSet<PyFunction>();
final Map<PyFunction, Boolean> emptyFunctions = new HashMap<PyFunction, Boolean>();
for (PsiElement element : myUnusedElements) {
boolean ignoreUnused = false;
@@ -275,6 +274,14 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
else if (ignoreUnusedParameters(func, functionsWithInheritors)) {
continue;
}
Boolean isEmpty = emptyFunctions.get(func);
if (isEmpty == null) {
isEmpty = isEmptyFunction(func);
emptyFunctions.put(func, isEmpty);
}
if (isEmpty) {
continue;
}
}
final LocalQuickFix[] fixes = mayBeField
? new LocalQuickFix[] { new AddFieldQuickFix(name, containingClass, name) }
@@ -393,4 +400,40 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
return getName();
}
}
private static boolean isEmptyFunction(@NotNull PyFunction f) {
final PyStatementList statementList = f.getStatementList();
if (statementList == null) {
return true;
}
final PyStatement[] statements = statementList.getStatements();
if (statements.length == 0) {
return true;
}
else if (statements.length == 1) {
if (isStringLiteral(statements[0]) || isPassOrRaise(statements[0])) {
return true;
}
}
else if (statements.length == 2) {
if (isStringLiteral(statements[0]) && (isPassOrRaise(statements[1]))) {
return true;
}
}
return false;
}
private static boolean isPassOrRaise(PyStatement stmt) {
return stmt instanceof PyPassStatement || stmt instanceof PyRaiseStatement;
}
private static boolean isStringLiteral(PyStatement stmt) {
if (stmt instanceof PyExpressionStatement) {
final PyExpression expr = ((PyExpressionStatement)stmt).getExpression();
if (expr instanceof PyStringLiteralExpression) {
return true;
}
}
return false;
}
}
@@ -1,3 +1,3 @@
class A:
def __init__(self, <weak_warning descr="Parameter 'foo' value is not used">f<caret>oo</weak_warning>):
pass
print('hello')
@@ -1,3 +1,3 @@
class A:
def __init__(self, <weak_warning descr="Parameter 'foo' value is not used">f<caret>oo=True</weak_warning>):
pass
print('hello')
@@ -1,3 +1,4 @@
class A:
def __init__(self, foo=True):
print('hello')
self.foo = foo
@@ -1,3 +1,4 @@
class A:
def __init__(self, foo):
print('hello')
self.foo = foo
@@ -112,7 +112,7 @@ def test():
print v
def foo(<weak_warning descr="Parameter 'a' value is not used">a = 123</weak_warning>): # fail Do not use getText() as parameter name
pass
print('hello')
def loopie():
for x in range(5): pass
@@ -123,9 +123,9 @@ def locals_inside():
do_smth_with(locals())
class Stat(object):
@staticmethod
def woof(<weak_warning descr="Parameter 'dog' value is not used">dog="bark"</weak_warning>): # fail
pass
@staticmethod
def woof(<weak_warning descr="Parameter 'dog' value is not used">dog="bark"</weak_warning>): # fail
print('hello')
class A:
def __init__(self, *args): #pass
@@ -261,3 +261,25 @@ def test_only_name_in_local_class():
class <weak_warning descr="Local class 'C' is not used">C</weak_warning>:
pass
return x
# PY-7028
def test_unused_params_in_empty_function_1(x, y, z):
pass
# PY-7028
def test_unused_params_in_empty_function_2(x, y, z):
raise Exception()
# PY-7028
def test_unused_params_in_empty_function_3(x, y, z):
"""Docstring."""
# PY-7028
def test_unused_params_in_empty_function_4(x, y, z):
"""Docstring."""
raise Exception()