From 8a830c1018dd0225d06d36e330561cd36d79564d Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 24 Aug 2020 14:05:14 +0700 Subject: [PATCH] ReturnSeparatedFromComputationInspection: do not process if variable and return are in different scopes Fixes EA-237085 - assert: ControlFlowUtil.isVariableReadInFinally GitOrigin-RevId: 1f48700f3f08faf96f12b440b3fd54d735c50620 --- ...eturnSeparatedFromComputationInspection.java | 7 ++++++- .../beforeFinallyInLambda.java | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/beforeFinallyInLambda.java diff --git a/java/java-impl/src/com/intellij/codeInspection/intermediaryVariable/ReturnSeparatedFromComputationInspection.java b/java/java-impl/src/com/intellij/codeInspection/intermediaryVariable/ReturnSeparatedFromComputationInspection.java index e8e8a2f02ac6..35510ceba84c 100644 --- a/java/java-impl/src/com/intellij/codeInspection/intermediaryVariable/ReturnSeparatedFromComputationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/intermediaryVariable/ReturnSeparatedFromComputationInspection.java @@ -69,7 +69,12 @@ public class ReturnSeparatedFromComputationInspection extends AbstractBaseJavaLo final PsiVariable returnedVariable = (PsiVariable)resolved; final PsiCodeBlock variableScope = getVariableScopeBlock(returnedVariable); if (variableScope != null) { - return new ReturnContext(returnStatement, returnScope, returnType, refactoredStatement, returnedVariable, variableScope); + PsiElement variableMethod = PsiTreeUtil.getParentOfType(variableScope, PsiMethod.class, PsiLambdaExpression.class); + PsiElement returnMethod = PsiTreeUtil.getParentOfType(returnScope, PsiMethod.class, PsiLambdaExpression.class); + if (variableMethod == returnMethod) { + return new ReturnContext(returnStatement, returnScope, returnType, refactoredStatement, returnedVariable, + variableScope); + } } } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/beforeFinallyInLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/beforeFinallyInLambda.java new file mode 100644 index 000000000000..2de2439024be --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/beforeFinallyInLambda.java @@ -0,0 +1,17 @@ +// "Move 'return' closer to computation of the value of 'x'" "false" +class X { + int test() { + int x = 0; + Runnable r = () -> { + try { + if (r == null) { + x = 1; + } + return x; + } + finally { + System.out.println(x); + } + }; + } +} \ No newline at end of file