diff --git a/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java b/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java index ca1c502f430b..883a62326091 100644 --- a/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java +++ b/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java @@ -92,9 +92,23 @@ public class PyCodeFragmentUtil { } // If we see more than 1 outer instruction, controlflow is interrupted - if (outerInstructions.size() > 1){ + if (outerInstructions.size() > 2){ throw new CannotCreateCodeFragmentException(PyBundle.message("refactoring.extract.method.error.cannot.perform.refactoring.when.execution.flow.is.interrupted")); } + if (outerInstructions.size() == 2){ + boolean errorFound = true; + 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; + } + } + if (errorFound){ + throw new CannotCreateCodeFragmentException(PyBundle.message("refactoring.extract.method.error.cannot.perform.refactoring.when.execution.flow.is.interrupted")); + } + } // Building code fragment final PyCodeFragmentBuilder builder = new PyCodeFragmentBuilder(owner, start, end);