mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
field can't be local if its initializer is not a constant
This commit is contained in:
@@ -160,7 +160,7 @@ public class FieldCanBeLocalInspection extends BaseLocalInspectionTool {
|
||||
final PsiElement resolved = readBeforeWrite.resolve();
|
||||
if (resolved instanceof PsiField) {
|
||||
final PsiField field = (PsiField)resolved;
|
||||
if (!(field.getType() instanceof PsiPrimitiveType) || getWrittenVariables(controlFlow, writtenVariables).contains(field)){
|
||||
if (!(field.getType() instanceof PsiPrimitiveType) || !PsiUtil.isConstantExpression(field.getInitializer()) || getWrittenVariables(controlFlow, writtenVariables).contains(field)){
|
||||
PsiElement parent = body.getParent();
|
||||
if (!(parent instanceof PsiMethod) ||
|
||||
!((PsiMethod)parent).isConstructor() ||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems/>
|
||||
@@ -0,0 +1,16 @@
|
||||
import java.util.*;
|
||||
|
||||
public class Outer {
|
||||
private final int value = calcValue();
|
||||
|
||||
int myValue;
|
||||
Set set = new HashSet();
|
||||
|
||||
int calcValue() {
|
||||
return set.size();
|
||||
}
|
||||
|
||||
void initMyValue() {
|
||||
myValue = value;
|
||||
}
|
||||
}
|
||||
@@ -28,4 +28,5 @@ public class FieldCanBeLocalTest extends InspectionTestCase {
|
||||
public void testLocalVar2InnerClass() throws Exception { doTest(); }
|
||||
public void testStateField() throws Exception { doTest(); }
|
||||
public void testLocalStateVar2InnerClass() throws Exception { doTest(); }
|
||||
public void testNotConstantInitializer() throws Exception {doTest();}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user