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);
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;
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));
+ }
}
}
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
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
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
doTest();
}
- private void doTest(LocalInspectionTool... tools) {
+ private void doTest(InspectionProfileEntry... tools) {
myFixture.enableInspections(tools);
myFixture.testHighlighting(true, false, false, getTestName(false) + ".groovy");
}
''')
myFixture.testHighlighting(true, false, false)
}
+
+ public void testGloballyUnusedSymbols() {
+ doTest(new GroovyUnusedDeclarationInspection(), new UnusedDeclarationInspection())
+ }
}
\ No newline at end of file
--- /dev/null
+
+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