mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
lambda: treat fields used for write inside lambda same way as anonymous classes (IDEA-92085)
This commit is contained in:
+10
-1
@@ -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());
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
</problems>
|
||||
+13
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user