mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Add an option to ignore unused variables starting with '_' (PY-27435)
This commit is contained in:
@@ -35,22 +35,24 @@ public class PyUnusedLocalInspection extends PyInspection {
|
||||
public boolean ignoreTupleUnpacking = true;
|
||||
public boolean ignoreLambdaParameters = true;
|
||||
public boolean ignoreLoopIterationVariables = true;
|
||||
public boolean ignoreVariablesStartingWithUnderscore = true;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
@Nls
|
||||
public String getDisplayName() {
|
||||
return PyBundle.message("INSP.NAME.unused");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder,
|
||||
final boolean isOnTheFly,
|
||||
@NotNull LocalInspectionToolSession session) {
|
||||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
|
||||
final PyUnusedLocalInspectionVisitor visitor = new PyUnusedLocalInspectionVisitor(holder,
|
||||
session,
|
||||
ignoreTupleUnpacking,
|
||||
ignoreLambdaParameters,
|
||||
ignoreLoopIterationVariables);
|
||||
ignoreLoopIterationVariables,
|
||||
ignoreVariablesStartingWithUnderscore);
|
||||
// buildVisitor() will be called on injected files in the same session - don't overwrite if we already have one
|
||||
final PyUnusedLocalInspectionVisitor existingVisitor = session.getUserData(KEY);
|
||||
if (existingVisitor == null) {
|
||||
@@ -70,10 +72,11 @@ public class PyUnusedLocalInspection extends PyInspection {
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
|
||||
final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
|
||||
panel.addCheckbox("Ignore variables used in tuple unpacking", "ignoreTupleUnpacking");
|
||||
panel.addCheckbox("Ignore lambda parameters", "ignoreLambdaParameters");
|
||||
panel.addCheckbox("Ignore range iteration variables", "ignoreLoopIterationVariables");
|
||||
panel.addCheckbox("Ignore variables starting with '_'", "ignoreVariablesStartingWithUnderscore");
|
||||
return panel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
|
||||
private final boolean myIgnoreTupleUnpacking;
|
||||
private final boolean myIgnoreLambdaParameters;
|
||||
private final boolean myIgnoreRangeIterationVariables;
|
||||
private final boolean myIgnoreVariablesStartingWithUnderscore;
|
||||
private final HashSet<PsiElement> myUnusedElements;
|
||||
private final HashSet<PsiElement> myUsedElements;
|
||||
|
||||
@@ -69,11 +70,13 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
|
||||
@NotNull LocalInspectionToolSession session,
|
||||
boolean ignoreTupleUnpacking,
|
||||
boolean ignoreLambdaParameters,
|
||||
boolean ignoreRangeIterationVariables) {
|
||||
boolean ignoreRangeIterationVariables,
|
||||
boolean ignoreVariablesStartingWithUnderscore) {
|
||||
super(holder, session);
|
||||
myIgnoreTupleUnpacking = ignoreTupleUnpacking;
|
||||
myIgnoreLambdaParameters = ignoreLambdaParameters;
|
||||
myIgnoreRangeIterationVariables = ignoreRangeIterationVariables;
|
||||
myIgnoreVariablesStartingWithUnderscore = ignoreVariablesStartingWithUnderscore;
|
||||
myUnusedElements = new HashSet<>();
|
||||
myUsedElements = new HashSet<>();
|
||||
}
|
||||
@@ -395,7 +398,7 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL, null, new ReplaceWithWildCard());
|
||||
}
|
||||
}
|
||||
else {
|
||||
else if (!myIgnoreVariablesStartingWithUnderscore || !name.startsWith(PyNames.UNDERSCORE)) {
|
||||
registerWarning(element, PyBundle.message("INSP.unused.locals.local.variable.isnot.used", name), new PyRemoveStatementQuickFix());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
def foo():
|
||||
l = [42 for <weak_warning descr="Local variable '_a' value is not used">_a</weak_warning> in xrange(100)]
|
||||
l = [42 for _a in xrange(100)]
|
||||
print(l)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
def foo():
|
||||
<weak_warning descr="Local variable '_var' value is not used">_var</weak_warning> = 1
|
||||
@@ -87,6 +87,7 @@ public class PyUnusedLocalInspectionTest extends PyInspectionTestCase {
|
||||
}
|
||||
|
||||
// PY-3996
|
||||
// PY-27435
|
||||
public void testUnderscorePrefixed() {
|
||||
doTest();
|
||||
}
|
||||
@@ -101,6 +102,13 @@ public class PyUnusedLocalInspectionTest extends PyInspectionTestCase {
|
||||
runWithLanguageLevel(LanguageLevel.PYTHON37, this::doTest);
|
||||
}
|
||||
|
||||
// PY-27435
|
||||
public void testVariableStartingWithUnderscore() {
|
||||
final PyUnusedLocalInspection inspection = new PyUnusedLocalInspection();
|
||||
inspection.ignoreVariablesStartingWithUnderscore = false;
|
||||
doTest(inspection);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Class<? extends PyInspection> getInspectionClass() {
|
||||
|
||||
Reference in New Issue
Block a user