diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index 426d813e3ad5..d2c06fc23546 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -394,7 +394,7 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { boolean isStaticallyTrue = false; if (condition != null) { condition.accept(this); - isStaticallyTrue = (PyConstantExpressionEvaluator.evaluate(condition) == Boolean.TRUE); + isStaticallyTrue = PyConstantExpressionEvaluator.evaluateBoolean(condition, false); } final Instruction head = myBuilder.prevInstruction; final PyElsePart elsePart = node.getElsePart(); @@ -752,7 +752,7 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { super.visitPyAssertStatement(node); final PyExpression[] args = node.getArguments(); // assert False - if (args.length >= 1 && PyConstantExpressionEvaluator.evaluate(args[0]) == Boolean.FALSE) { + if (args.length >= 1 && !PyConstantExpressionEvaluator.evaluateBoolean(args[0], true)) { abruptFlow(node); return; } diff --git a/python/src/com/jetbrains/python/psi/impl/PyConstantExpressionEvaluator.java b/python/src/com/jetbrains/python/psi/impl/PyConstantExpressionEvaluator.java index f9b38cfbe1e0..025f7268f200 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyConstantExpressionEvaluator.java +++ b/python/src/com/jetbrains/python/psi/impl/PyConstantExpressionEvaluator.java @@ -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. @@ -62,6 +62,9 @@ public class PyConstantExpressionEvaluator { if (result instanceof Boolean) { return (Boolean)result; } + else if (result instanceof Integer) { + return ((Integer)result) != 0; + } else { return defaultValue; } diff --git a/python/testData/inspections/PyUnboundLocalVariableInspection/WhileOneBreak.py b/python/testData/inspections/PyUnboundLocalVariableInspection/WhileOneBreak.py new file mode 100644 index 000000000000..21d867c7cdac --- /dev/null +++ b/python/testData/inspections/PyUnboundLocalVariableInspection/WhileOneBreak.py @@ -0,0 +1,4 @@ +while 1: + a = 1 + break +print(a) # pass diff --git a/python/testData/refactoring/inlinelocal/doubleDefinition.before.py b/python/testData/refactoring/inlinelocal/doubleDefinition.before.py index e8d3569d180e..0c691345249f 100644 --- a/python/testData/refactoring/inlinelocal/doubleDefinition.before.py +++ b/python/testData/refactoring/inlinelocal/doubleDefinition.before.py @@ -1,5 +1,5 @@ foo = 10 -while 1: +if 5 in [1, 2, 3]: foo += 10 b = foo \ No newline at end of file diff --git a/python/testData/refactoring/inlinelocal/noDominator.before.py b/python/testData/refactoring/inlinelocal/noDominator.before.py index df46535959be..e46604a78a0b 100644 --- a/python/testData/refactoring/inlinelocal/noDominator.before.py +++ b/python/testData/refactoring/inlinelocal/noDominator.before.py @@ -1,5 +1,5 @@ foo = 10 -while 1: +if 5 in [1, 2, 3]: foo = foo + 10 b = foo \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnboundLocalVariableInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnboundLocalVariableInspectionTest.java index 5b4f9f8c0abe..7d54cc5270fc 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnboundLocalVariableInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnboundLocalVariableInspectionTest.java @@ -188,6 +188,10 @@ public class PyUnboundLocalVariableInspectionTest extends PyTestCase { doTest(); } + public void testWhileOneBreak() { + doTest(); + } + private void doTest() { myFixture.configureByFile(TEST_DIRECTORY + getTestName(false) + ".py"); myFixture.enableInspections(PyUnboundLocalVariableInspection.class);