highlight globally unused groovy properties (IDEA-75803)
authorpeter <peter@jetbrains.com>
Mon, 30 Jan 2012 19:33:23 +0000 (20:33 +0100)
committerpeter <peter@jetbrains.com>
Tue, 31 Jan 2012 17:30:25 +0000 (18:30 +0100)
java/java-impl/src/com/intellij/codeInsight/daemon/impl/PostHighlightingPass.java
plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/local/GroovyPostHighlightingPass.java
plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy
plugins/groovy/testdata/highlighting/GloballyUnusedSymbols.groovy [new file with mode: 0644]

index 2c40a1aa2c7899ca0c8b3af4c6d50c0aee4e007b..531bc286acfd45de211f2adfde8e8b10cd88f5de 100644 (file)
@@ -447,15 +447,22 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
     else if (isImplicitUsage(field, progress)) {
       return null;
     }
-    else if (!myRefCountHolder.isReferenced(field) && weAreSureThereAreNoUsages(field, progress, helper)) {
-      if (field instanceof PsiEnumConstant && isEnumValuesMethodUsed(field, progress, helper)) {
-        return null;
-      }
+    else if (isFieldUnused(field, progress, helper)) {
       return formatUnusedSymbolHighlightInfo("field.is.not.used", field, "fields", myDeadCodeKey, myDeadCodeInfoType);
     }
     return null;
   }
 
+  public static boolean isFieldUnused(PsiField field, ProgressIndicator progress, GlobalUsageHelper helper) {
+    if (helper.isLocallyUsed(field) || !weAreSureThereAreNoUsages(field, progress, helper)) {
+      return false;
+    }
+    if (field instanceof PsiEnumConstant && isEnumValuesMethodUsed(field, progress, helper)) {
+      return false;
+    }
+    return true;
+  }
+
   private HighlightInfo suggestionsToMakeFieldUsed(final PsiField field, final PsiIdentifier identifier, final String message) {
     HighlightInfo highlightInfo = createUnusedSymbolInfo(identifier, message, HighlightInfoType.UNUSED_SYMBOL);
     QuickFixAction.registerQuickFixAction(highlightInfo, new RemoveUnusedVariableFix(field), myUnusedSymbolKey);
index 4dd5726262f3c307a5e5c1ac9ced643aa46f1c78..49c4c67fcec4bcbbefc9c74940af5359829e2f0a 100644 (file)
@@ -54,6 +54,7 @@ import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement;
 import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
 import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
 import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
 import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement;
@@ -125,6 +126,10 @@ public class GroovyPostHighlightingPass extends TextEditorHighlightingPass {
                   PostHighlightingPass.createUnusedSymbolInfo(nameId, (method.isConstructor() ? "Constructor" : "Method") +" " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL));
               }
             }
+            else if (element instanceof GrField && PostHighlightingPass.isFieldUnused((GrField)element, progress, usageHelper)) {
+              unusedDeclarations.add(
+                PostHighlightingPass.createUnusedSymbolInfo(nameId, "Property " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL));
+            }
           }
         }
         
index e72b91eb44d6c82127cb02f96dc322c491174f18..48231e862fe76fd37cddcdb1fdb29a67adf5786c 100644 (file)
@@ -16,7 +16,7 @@
 package org.jetbrains.plugins.groovy.lang;
 
 
-import com.intellij.codeInspection.LocalInspectionTool
+import com.intellij.codeInspection.InspectionProfileEntry
 import com.intellij.openapi.module.Module
 import com.intellij.openapi.roots.ContentEntry
 import com.intellij.openapi.roots.ModifiableRootModel
@@ -31,6 +31,7 @@ import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
 import com.siyeh.ig.junit.JUnitAbstractTestClassNamingConventionInspection
 import com.siyeh.ig.junit.JUnitTestClassNamingConventionInspection
 import org.jetbrains.annotations.NotNull
+import org.jetbrains.plugins.groovy.codeInspection.GroovyUnusedDeclarationInspection
 import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection
 import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyResultOfAssignmentUsedInspection
 import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyUncheckedAssignmentOfMemberOfRawTypeInspection
@@ -48,6 +49,7 @@ import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.Groov
 import org.jetbrains.plugins.groovy.codeInspection.unusedDef.UnusedDefInspection
 import org.jetbrains.plugins.groovy.util.TestUtils
 import org.jetbrains.plugins.groovy.codeInspection.bugs.*
+import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection
 
 /**
  * @author peter
@@ -83,7 +85,7 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
     doTest();
   }
 
-  private void doTest(LocalInspectionTool... tools) {
+  private void doTest(InspectionProfileEntry... tools) {
     myFixture.enableInspections(tools);
     myFixture.testHighlighting(true, false, false, getTestName(false) + ".groovy");
   }
@@ -659,4 +661,8 @@ List<?> list2
 ''')
     myFixture.testHighlighting(true, false, false)
   }
+
+  public void testGloballyUnusedSymbols() {
+    doTest(new GroovyUnusedDeclarationInspection(), new UnusedDeclarationInspection())
+  }
 }
\ No newline at end of file
diff --git a/plugins/groovy/testdata/highlighting/GloballyUnusedSymbols.groovy b/plugins/groovy/testdata/highlighting/GloballyUnusedSymbols.groovy
new file mode 100644 (file)
index 0000000..5ad85a2
--- /dev/null
@@ -0,0 +1,19 @@
+
+class <warning descr="Class UnusedClass is unused">UnusedClass</warning> {}
+class Bar {
+  int <warning descr="Property unusedProperty is unused">unusedProperty</warning> = 2
+  int usedProperty = 39
+  int usedProperty2 = 39
+  int usedProperty3 = 39
+  def <warning descr="Method unusedMethod is unused">unusedMethod</warning>() {}
+  Bar usedMethod() { this }
+
+  Bar getUsedPropertyGetter() {}
+
+  public static void main(String[] args) {}
+
+}
+println new Bar().usedMethod().usedProperty
+new Bar().setUsedProperty2 42
+println new Bar().getUsedProperty3()
+println new Bar().usedPropertyGetter