IDEA-160174 (Inspection incorrectly suggests to narrow variable scope)
authorBas Leijdekkers <basleijdekkers@gmail.com>
Mon, 26 Sep 2016 15:02:40 +0000 (17:02 +0200)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Mon, 26 Sep 2016 15:04:28 +0000 (17:04 +0200)
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/TooBroadScopeInspectionBase.java
plugins/InspectionGadgets/test/com/siyeh/igtest/dataflow/scope/TooBroadScope.java

index 85df4d8c50004da867e319f4246b0a4ed56d34f6..6240a909c6f436540d93c0f4f911561728f80f31 100644 (file)
@@ -135,6 +135,10 @@ public class TooBroadScopeInspectionBase extends BaseInspection {
     }
     if (expression instanceof PsiReferenceExpression) {
       final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)expression;
+      final PsiExpression qualifier = referenceExpression.getQualifierExpression();
+      if (!isMoveable(qualifier)) {
+        return false;
+      }
       final PsiElement target = referenceExpression.resolve();
       if (target instanceof PsiClass) {
         return true;
@@ -232,6 +236,8 @@ public class TooBroadScopeInspectionBase extends BaseInspection {
 
   private class TooBroadScopeVisitor extends BaseInspectionVisitor {
 
+    TooBroadScopeVisitor() {}
+
     @Override
     public void visitVariable(@NotNull PsiVariable variable) {
       super.visitVariable(variable);
index b2d31e3351fade1b0a8484ab7154d5298ca245dd..26e26ee1c010427bd3104f55a087a9596418f7a4 100644 (file)
@@ -206,4 +206,20 @@ public class TooBroadScope
             System.out.println(s);
         }
     }
+}
+class T {
+
+    private Object[] array = {};
+
+    public void foo(boolean value) {
+        final int size = array.length;
+
+        reinitArray();
+
+        System.out.println(size);
+    }
+
+    private void reinitArray() {
+        array = new String[5];
+    }
 }
\ No newline at end of file