field can be local extended: suggest read only fields used in e.g. constructor once...
authoranna <anna.kozlova@jetbrains.com>
Thu, 4 Mar 2010 11:40:40 +0000 (14:40 +0300)
committeranna <anna.kozlova@jetbrains.com>
Fri, 5 Mar 2010 11:07:36 +0000 (14:07 +0300)
java/java-impl/src/com/intellij/codeInspection/varScopeCanBeNarrowed/FieldCanBeLocalInspection.java
java/java-tests/testData/inspection/fieldCanBeLocal/innerClassConstructor/expected.xml [new file with mode: 0644]
java/java-tests/testData/inspection/fieldCanBeLocal/innerClassConstructor/src/Test.java [new file with mode: 0644]
java/java-tests/testData/inspection/fieldCanBeLocal/localVar2InnerClass/expected.xml [new file with mode: 0644]
java/java-tests/testData/inspection/fieldCanBeLocal/localVar2InnerClass/src/Test.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/codeInspection/FieldCanBeLocalTest.java

index 8596bb23ddfb1868796a1b33be5c4a9c7ee5c09e..d6c709e8fa87e9bf93456b9027048f6645acff6a 100644 (file)
@@ -153,17 +153,21 @@ public class FieldCanBeLocalInspection extends BaseLocalInspectionTool {
           }
         }
       }
+      final Collection<PsiVariable> writtenVariables = ControlFlowUtil.getWrittenVariables(controlFlow, 0, controlFlow.getSize(), false);
       final List<PsiReferenceExpression> readBeforeWrites = ControlFlowUtil.getReadBeforeWrite(controlFlow);
       for (final PsiReferenceExpression readBeforeWrite : readBeforeWrites) {
         final PsiElement resolved = readBeforeWrite.resolve();
         if (resolved instanceof PsiField) {
           final PsiField field = (PsiField)resolved;
-          PsiElement parent = body.getParent();
-          if (!(parent instanceof PsiMethod) ||
-              !((PsiMethod)parent).isConstructor() ||
-              field.getInitializer() == null ||
-              field.hasModifierProperty(PsiModifier.STATIC)) {
-            candidates.remove(field);
+          if (writtenVariables.contains(field)){
+            PsiElement parent = body.getParent();
+            if (!(parent instanceof PsiMethod) ||
+                !((PsiMethod)parent).isConstructor() ||
+                field.getInitializer() == null ||
+                field.hasModifierProperty(PsiModifier.STATIC) ||
+                !PsiTreeUtil.isAncestor(((PsiMethod)parent).getContainingClass(), field, true)) {
+              candidates.remove(field);
+            }
           }
         }
       }
diff --git a/java/java-tests/testData/inspection/fieldCanBeLocal/innerClassConstructor/expected.xml b/java/java-tests/testData/inspection/fieldCanBeLocal/innerClassConstructor/expected.xml
new file mode 100644 (file)
index 0000000..4704d91
--- /dev/null
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<problems/>
\ No newline at end of file
diff --git a/java/java-tests/testData/inspection/fieldCanBeLocal/innerClassConstructor/src/Test.java b/java/java-tests/testData/inspection/fieldCanBeLocal/innerClassConstructor/src/Test.java
new file mode 100644 (file)
index 0000000..56d4b9c
--- /dev/null
@@ -0,0 +1,11 @@
+public class Outer {
+  private int value = 0;
+
+  public class Inner {
+    private final int myValue;
+
+    public Inner() {
+      myValue = value++;
+    }
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/inspection/fieldCanBeLocal/localVar2InnerClass/expected.xml b/java/java-tests/testData/inspection/fieldCanBeLocal/localVar2InnerClass/expected.xml
new file mode 100644 (file)
index 0000000..c066f3d
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<problems>
+  <problem>
+    <file>Test.java</file>
+    <line>2</line>
+    <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Field can be local</problem_class>
+    <description>Field can be converted to a local variable</description>
+  </problem>
+</problems>
\ No newline at end of file
diff --git a/java/java-tests/testData/inspection/fieldCanBeLocal/localVar2InnerClass/src/Test.java b/java/java-tests/testData/inspection/fieldCanBeLocal/localVar2InnerClass/src/Test.java
new file mode 100644 (file)
index 0000000..d7b38e0
--- /dev/null
@@ -0,0 +1,11 @@
+public class Outer {
+  private int value = 0;
+
+  public class Inner {
+    private final int myValue;
+
+    public Inner() {
+      myValue = value;
+    }
+  }
+}
\ No newline at end of file
index f1c291663a9c0580047d614e82ae292a838a28cc..10630e3c49c4cbecdf1f5223e529165b6d158c85 100644 (file)
@@ -24,4 +24,6 @@ public class FieldCanBeLocalTest extends InspectionTestCase {
   public void testConstructor () throws Exception { doTest(); }
   public void testStaticFinal() throws Exception { doTest(); }
   public void testStaticAccess() throws Exception { doTest(); }
+  public void testInnerClassConstructor() throws Exception { doTest(); }
+  public void testLocalVar2InnerClass() throws Exception { doTest(); }
 }