mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
field can be local: extend "immutable" types (IDEA-63561)
This commit is contained in:
+12
-1
@@ -31,6 +31,7 @@ import com.intellij.openapi.editor.ScrollType;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.JDOMExternalizableStringList;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.util.WriteExternalException;
|
||||
@@ -190,7 +191,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) || !PsiUtil.isConstantExpression(field.getInitializer()) || getWrittenVariables(controlFlow, writtenVariables).contains(field)){
|
||||
if (!isImmutableState(field.getType()) || !PsiUtil.isConstantExpression(field.getInitializer()) || getWrittenVariables(controlFlow, writtenVariables).contains(field)){
|
||||
PsiElement parent = body.getParent();
|
||||
if (!(parent instanceof PsiMethod) ||
|
||||
!((PsiMethod)parent).isConstructor() ||
|
||||
@@ -208,6 +209,16 @@ public class FieldCanBeLocalInspection extends BaseLocalInspectionTool {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isImmutableState(PsiType type) {
|
||||
if (type instanceof PsiPrimitiveType) {
|
||||
return true;
|
||||
}
|
||||
if (PsiPrimitiveType.getUnboxedType(type) != null) {
|
||||
return true;
|
||||
}
|
||||
return Comparing.strEqual(CommonClassNames.JAVA_LANG_STRING, type.getCanonicalText());
|
||||
}
|
||||
|
||||
private static Collection<PsiVariable> getWrittenVariables(ControlFlow controlFlow, Ref<Collection<PsiVariable>> writtenVariables) {
|
||||
if (writtenVariables.get() == null) {
|
||||
writtenVariables.set(ControlFlowUtil.getWrittenVariables(controlFlow, 0, controlFlow.getSize(), false));
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>2</line>
|
||||
<description>Field can be converted to a local variable</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class Test {
|
||||
private String f = "";
|
||||
|
||||
void foo () {
|
||||
f.substring(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,6 +30,7 @@ public class FieldCanBeLocalTest extends InspectionTestCase {
|
||||
public void testLocalStateVar2InnerClass() throws Exception { doTest(); }
|
||||
public void testNotConstantInitializer() throws Exception {doTest();}
|
||||
public void testInnerClassFieldInitializer() throws Exception {doTest();}
|
||||
public void testFieldWithImmutableType() 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