highlight globally unused groovy properties (IDEA-75803)

This commit is contained in:
peter
2012-01-31 18:30:25 +01:00
parent 9dfa66531e
commit ea7151df75
4 changed files with 43 additions and 6 deletions
@@ -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);
@@ -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));
}
}
}
@@ -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())
}
}
@@ -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