diff --git a/python/src/com/jetbrains/python/inspections/PyRedeclarationInspection.java b/python/src/com/jetbrains/python/inspections/PyRedeclarationInspection.java index 6b1268c12e48..007ee734bf61 100644 --- a/python/src/com/jetbrains/python/inspections/PyRedeclarationInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyRedeclarationInspection.java @@ -74,8 +74,7 @@ public class PyRedeclarationInspection extends PyInspection { } private static boolean isConditional(@NotNull PsiElement node) { - return PsiTreeUtil.getParentOfType(node, PyIfStatement.class, PyConditionalExpression.class, PyLoopStatement.class, - PyComprehensionElement.class, PyTryExceptStatement.class) != null; + return PsiTreeUtil.getParentOfType(node, PyIfStatement.class, PyConditionalExpression.class, PyTryExceptStatement.class) != null; } private static boolean isDecorated(@NotNull PyDecoratable node) { @@ -118,17 +117,20 @@ public class PyRedeclarationInspection extends PyInspection { if (rwInstruction.getAccess().isWriteAccess()) { final List quickFixes = new ArrayList(); final PsiElement originalElement = rwInstruction.getElement(); - if (originalElement != null && suggestRename(element, originalElement)) { - quickFixes.add(new PyRenameElementQuickFix()); + if (originalElement != null && originalElement != element) { + if (suggestRename(element, originalElement)) { + quickFixes.add(new PyRenameElementQuickFix()); + } + final PsiElement identifier = element.getNameIdentifier(); + registerProblem(identifier != null ? identifier : element, + PyBundle.message("INSP.redeclared.name", name), + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, + null, + quickFixes.toArray(new LocalQuickFix[quickFixes.size()])); + return ControlFlowUtil.Operation.BREAK; } - final PsiElement identifier = element.getNameIdentifier(); - registerProblem(identifier != null ? identifier : element, - PyBundle.message("INSP.redeclared.name", name), - ProblemHighlightType.GENERIC_ERROR_OR_WARNING, - null, - quickFixes.toArray(new LocalQuickFix[quickFixes.size()])); } - return ControlFlowUtil.Operation.BREAK; + return ControlFlowUtil.Operation.CONTINUE; } } return ControlFlowUtil.Operation.NEXT; diff --git a/python/testData/inspections/PyRedeclarationInspection/test.py b/python/testData/inspections/PyRedeclarationInspection/test.py index 58a1f1541abf..a8895dade5f5 100644 --- a/python/testData/inspections/PyRedeclarationInspection/test.py +++ b/python/testData/inspections/PyRedeclarationInspection/test.py @@ -43,7 +43,7 @@ def test_local_variable(): x = 2 -def conditional(c): +def test_conditional(c): def foo(): pass @@ -51,12 +51,39 @@ def conditional(c): def foo(): pass - while c: - def foo(): - pass - try: def foo(): pass except: pass + + +def test_while_loop(c): + def foo(): + pass + + while c: + def foo(): + pass + + +class TestForLoopNoRedeclaraion: + for foo in [1, 2, 3]: + x = 1 + + +class TestForLoopTarget: + def foo(): + pass + + for foo in [1, 2, 3]: + x = 1 + + +class TestForLoopBody: + def foo(): + pass + + for _ in [1, 2, 3]: + def foo(): + pass diff --git a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java index d75aa2b88956..f9e844134b2c 100644 --- a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java @@ -433,7 +433,7 @@ public class PyQuickFixTest extends PyTestCase { public void testRenameShadowingBuiltins() { final String fileName = "RenameShadowingBuiltins.py"; myFixture.configureByFile(fileName); - myFixture.enableInspections(PyShadowingNamesInspection.class); + myFixture.enableInspections(PyShadowingBuiltinsInspection.class); myFixture.checkHighlighting(true, false, true); final IntentionAction intentionAction = myFixture.getAvailableIntention("Rename element"); assertNotNull(intentionAction);