lambda effectively final: ignore variables defined inside lambda (IDEA-89818)

This commit is contained in:
Anna Kozlova
2012-08-07 20:17:00 +04:00
parent 4ab8573957
commit bee0bd2963
2 changed files with 22 additions and 1 deletions
@@ -694,7 +694,7 @@ public class HighlightControlFlowUtil {
return highlightInfo;
} else {
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(context, PsiLambdaExpression.class);
if (lambdaExpression != null) {
if (lambdaExpression != null && !PsiTreeUtil.isAncestor(lambdaExpression, variable, true)) {
boolean effectivelyFinal;
if (variable instanceof PsiParameter) {
final PsiElement parent = variable.getParent();
@@ -70,3 +70,24 @@ public class XXX {
foo(() -> <error descr="Variable used in lambda expression should be effectively final">y</error>=1);
}
}
class Sample {
public static void main(String[] args) {
Runnable runnable = () -> {
Integer i;
if (true) {
i = 111;
System.out.println(i);
}
};
Runnable runnable2 = () -> {
Integer i2 = 333;
i2 = 444;
System.out.println(i2);
};
runnable.run(); // prints 111
runnable2.run(); // prints 444
}
}