From da1b4990913ebd2e9249449187d62bb935c30e5d Mon Sep 17 00:00:00 2001 From: Oleg Shpynov Date: Wed, 16 Jun 2010 17:49:50 +0400 Subject: [PATCH] PY-992 Extract Method doesn't handle conditional returns correctly --- .../codeInsight/codeFragment/PyCodeFragmentUtil.java | 9 ++++++--- .../extractmethod/conditionalreturn.before.py | 7 +++++++ .../python/refactoring/PyExtractMethodTest.java | 5 +++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 python/testData/refactoring/extractmethod/conditionalreturn.before.py diff --git a/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java b/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java index 883a62326091..241703071895 100644 --- a/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java +++ b/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java @@ -100,9 +100,12 @@ public class PyCodeFragmentUtil { for (Instruction outerInstruction : outerInstructions) { // Here we check control flow when for statement content is beeing extracted if (outerInstruction instanceof ReadWriteInstruction && - PyForStatementNavigator.getPyForStatementByIterable(outerInstruction.getElement())!=null){ - errorFound = false; - break; + PyForStatementNavigator.getPyForStatementByIterable(outerInstruction.getElement())!=null) { + // In case when return instruction is inside + if (!returnInstructionInside){ + errorFound = false; + break; + } } } if (errorFound){ diff --git a/python/testData/refactoring/extractmethod/conditionalreturn.before.py b/python/testData/refactoring/extractmethod/conditionalreturn.before.py new file mode 100644 index 000000000000..ea7119b33e3a --- /dev/null +++ b/python/testData/refactoring/extractmethod/conditionalreturn.before.py @@ -0,0 +1,7 @@ +class Conference(object): + def __init__(self): + self.talks = [] + + def getTalkAt(self, hour): + for start, end, name in self.talks: + if hour >= start and hour < end: return name \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyExtractMethodTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyExtractMethodTest.java index c04760dd7c97..6a0334228c5a 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyExtractMethodTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyExtractMethodTest.java @@ -126,4 +126,9 @@ public class PyExtractMethodTest extends LightMarkedTestCase { public void testClass() throws Throwable { doTest("context/class.before.py", "bar", "context/class.after.py"); } + + public void testConditionalReturn() throws Throwable { + doTest("conditionalreturn.before.py", "bar", "Cannot perform refactoring when execution flow is interrupted"); + } + }