ReturnSeparatedFromComputationInspection: do not process if variable and return are in different scopes

Fixes EA-237085 - assert: ControlFlowUtil.isVariableReadInFinally

GitOrigin-RevId: 1f48700f3f08faf96f12b440b3fd54d735c50620
This commit is contained in:
Tagir Valeev
2020-08-24 10:42:56 +00:00
committed by intellij-monorepo-bot
parent 997f2ec6d0
commit 8a830c1018
2 changed files with 23 additions and 1 deletions
@@ -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);
}
}
}
}
@@ -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 <caret>x;
}
finally {
System.out.println(x);
}
};
}
}