PY-19492 Fixed: Magic method parameter value is flagged as unused but cannot be removed because it's required by Python

Update PyUnusedLocalInspectionVisitor to ignore builtin method parameters (except __init__)
This commit is contained in:
Semyon Proshev
2016-08-01 21:28:25 +03:00
parent 08ce043366
commit c022ac4f03
2 changed files with 26 additions and 2 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@ import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.Function;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.codeInsight.controlflow.ControlFlowCache;
@@ -131,6 +130,9 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
if (PyAugAssignmentStatementNavigator.getStatementByTarget(element) != null) {
continue;
}
if (parameterInMethodWithFixedSignature(owner, element)) {
continue;
}
if (!myUsedElements.contains(element)) {
myUnusedElements.add(element);
}
@@ -138,6 +140,19 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
}
}
private static boolean parameterInMethodWithFixedSignature(@NotNull ScopeOwner owner, @NotNull PsiElement element) {
if (owner instanceof PyFunction && element instanceof PyParameter) {
final PyFunction function = (PyFunction)owner;
final String functionName = function.getName();
return !PyNames.INIT.equals(functionName) &&
function.getContainingClass() != null &&
PyNames.getBuiltinMethods(LanguageLevel.forElement(function)).containsKey(functionName);
}
return false;
}
private void collectUsedReads(final ScopeOwner owner) {
final Instruction[] instructions = ControlFlowCache.getControlFlow(owner).getInstructions();
for (int i = 0; i < instructions.length; i++) {
@@ -320,3 +320,12 @@ def test_unused_empty_init_parameter():
# PY-14429
def test_used_local_augmented_assignment(x, y):
x += y
# PY-19492
class A:
def __exit__(self, exc_type, exc_val, exc_tb):
print(exc_type)
def __eq__(self, other):
return False