lambda: treat fields used for write inside lambda same way as anonymous classes (IDEA-92085)

This commit is contained in:
anna
2012-09-26 20:37:19 +02:00
parent a5ecb6b068
commit 54372474cc
5 changed files with 33 additions and 10 deletions
@@ -152,6 +152,15 @@ public class FieldCanBeLocalInspection extends BaseLocalInspectionTool {
}
}
@Override
public void visitLambdaExpression(PsiLambdaExpression expression) {
super.visitLambdaExpression(expression);
final PsiElement body = expression.getBody();
if (body != null) {
checkCodeBlock(body, candidates, usedFields);
}
}
@Override
public void visitClassInitializer(PsiClassInitializer initializer) {
super.visitClassInitializer(initializer);
@@ -160,7 +169,7 @@ public class FieldCanBeLocalInspection extends BaseLocalInspectionTool {
});
}
private static void checkCodeBlock(final PsiCodeBlock body, final Set<PsiField> candidates, Set<PsiField> usedFields) {
private static void checkCodeBlock(final PsiElement body, final Set<PsiField> candidates, Set<PsiField> usedFields) {
try {
final ControlFlow controlFlow = ControlFlowFactory.getInstance(body.getProject()).getControlFlow(body, AllVariablesControlFlowPolicy.getInstance());
final List<PsiVariable> usedVars = ControlFlowUtil.getUsedVariables(controlFlow, 0, controlFlow.getSize());
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -0,0 +1,13 @@
class Test {
private int f;
void foo () {
f = 0;
int k = f;
}
int bar () {
f = 5;
return f;
}
}
@@ -1,13 +1,10 @@
class Test {
private int f;
private int f;
void foo () {
f = 0;
int k = f;
}
public void bar() {
foo(() -> {f++;});
}
int bar () {
f = 5;
return f;
}
private void foo(Runnable r) {
}
}
@@ -32,6 +32,7 @@ public class FieldCanBeLocalTest extends InspectionTestCase {
public void testInnerClassFieldInitializer() throws Exception {doTest();}
public void testFieldUsedInConstantInitialization() throws Exception {doTest();}
public void testFieldWithImmutableType() throws Exception {doTest();}
public void testFieldUsedForWritingInLambda() throws Exception {doTest();}
public void testIgnoreAnnotated() throws Exception {
final FieldCanBeLocalInspection inspection = new FieldCanBeLocalInspection();
inspection.EXCLUDE_ANNOS.add(Deprecated.class.getName());