From 7ce4f9b7e2f28e34d5b4d007d010f8b29133921b Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Tue, 3 Apr 2012 18:00:25 +0400 Subject: [PATCH] Fixed highlighting of unused local classes (PY-5086) --- python/src/com/jetbrains/python/PyBundle.properties | 1 + .../inspections/PyUnusedLocalInspectionVisitor.java | 13 ++++++++++--- .../PyUnusedLocalVariableInspection/test.py | 8 ++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index 41acb41e7d88..28b0d23f837b 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -277,6 +277,7 @@ INSP.unused.locals.parameter.isnot.used=Parameter ''{0}'' value is not used INSP.unused.locals.local.variable.isnot.used=Local variable ''{0}'' value is not used INSP.unused.locals.replace.with.wildcard=Replace with _ INSP.unused.locals.local.function.isnot.used=Local function ''{0}'' is not used +INSP.unused.locals.local.class.isnot.used=Local class ''{0}'' is not used # PyUnboundLocalVariableInspection INSP.NAME.unbound=Unbound local variable diff --git a/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java b/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java index 4ca52375ce62..e9635044b672 100644 --- a/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java +++ b/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java @@ -226,15 +226,22 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor { } if (ignoreUnused) continue; - // Local function - if (element instanceof PyFunction){ + if (element instanceof PyFunction) { + // Local function final PsiElement nameIdentifier = ((PyFunction)element).getNameIdentifier(); registerWarning(nameIdentifier == null ? element : nameIdentifier, PyBundle.message("INSP.unused.locals.local.function.isnot.used", ((PyFunction)element).getName())); } - // Local variable or parameter + else if (element instanceof PyClass) { + // Local class + final PyClass cls = (PyClass)element; + final PsiElement name = cls.getNameIdentifier(); + registerWarning(name != null ? name : element, + PyBundle.message("INSP.unused.locals.local.class.isnot.used", cls.getName())); + } else { + // Local variable or parameter String name = element.getText(); if (element instanceof PyNamedParameter || element.getParent() instanceof PyNamedParameter) { PyNamedParameter namedParameter = element instanceof PyNamedParameter diff --git a/python/testData/inspections/PyUnusedLocalVariableInspection/test.py b/python/testData/inspections/PyUnusedLocalVariableInspection/test.py index c6b8cc2ebaf2..1aaa872d76f5 100644 --- a/python/testData/inspections/PyUnusedLocalVariableInspection/test.py +++ b/python/testData/inspections/PyUnusedLocalVariableInspection/test.py @@ -253,3 +253,11 @@ def test_same_named_variable_inside_class(): def a(self): print(a) return C + + +# PY-5086 +def test_only_name_in_local_class(): + x = 1 + class C: + pass + return x