diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/unusedDef/UnusedDefInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/unusedDef/UnusedDefInspection.java index c6cec4247bfe..fb8e2f0e6c12 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/unusedDef/UnusedDefInspection.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/unusedDef/UnusedDefInspection.java @@ -33,6 +33,7 @@ import gnu.trove.TObjectProcedure; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle; import org.jetbrains.plugins.groovy.codeInspection.GroovyLocalInspectionBase; import org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner; @@ -146,7 +147,7 @@ public class UnusedDefInspection extends GroovyLocalInspectionBase { }); } - private boolean isUsedInToplevelFlowOnly(PsiElement element) { + private static boolean isUsedInToplevelFlowOnly(PsiElement element) { GrVariable var = null; if (element instanceof GrVariable) { var = (GrVariable) element; @@ -159,7 +160,7 @@ public class UnusedDefInspection extends GroovyLocalInspectionBase { final GroovyPsiElement scope = getScope(var); if (scope == null) { PsiFile file = var.getContainingFile(); - LOG.error(file == null ? "no file???" : DebugUtil.psiToString(file, true, false)); + LOG.error(file == null ? "no file??? var of type"+ var.getClass().getCanonicalName() : DebugUtil.psiToString(file, true, false)); } return ReferencesSearch.search(var, new LocalSearchScope(scope)).forEach(new Processor() { @@ -172,11 +173,12 @@ public class UnusedDefInspection extends GroovyLocalInspectionBase { return true; } - private GroovyPsiElement getScope(PsiElement var) { - return PsiTreeUtil.getParentOfType(var, GrClosableBlock.class, GrMethod.class, GrClassInitializer.class, GroovyFileBase.class); + @Nullable + private static GroovyPsiElement getScope(PsiElement var) { + return PsiTreeUtil.getContextOfType(var, GrClosableBlock.class, GrMethod.class, GrClassInitializer.class, GroovyFileBase.class); } - private boolean isLocalAssignment(PsiElement element) { + private static boolean isLocalAssignment(PsiElement element) { if (element instanceof GrVariable) { return isLocalVariable((GrVariable) element, false); } else if (element instanceof GrReferenceExpression) { @@ -187,11 +189,8 @@ public class UnusedDefInspection extends GroovyLocalInspectionBase { return false; } - private boolean isLocalVariable(GrVariable var, boolean parametersAllowed) { - if (var instanceof GrField) return false; - else if (var instanceof GrParameter && !parametersAllowed) return false; - - return true; + private static boolean isLocalVariable(GrVariable var, boolean parametersAllowed) { + return !(var instanceof GrField || var instanceof GrParameter && !parametersAllowed); } public boolean isEnabledByDefault() { diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/synthetic/ClosureSyntheticParameter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/synthetic/ClosureSyntheticParameter.java index 092ce0ec2eee..de967e9c9f7e 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/synthetic/ClosureSyntheticParameter.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/synthetic/ClosureSyntheticParameter.java @@ -79,11 +79,6 @@ public class ClosureSyntheticParameter extends GrLightParameter implements Navig return myClosure; } - @Override - public PsiElement getContext() { - return myClosure; - } - @Override public GrExpression getDefaultInitializer() { return GroovyPsiElementFactory.getInstance(getProject()).createExpressionFromText("null"); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/synthetic/GrLightParameter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/synthetic/GrLightParameter.java index ea8b68dd1ec4..e9001f166b08 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/synthetic/GrLightParameter.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/synthetic/GrLightParameter.java @@ -16,6 +16,7 @@ package org.jetbrains.plugins.groovy.lang.psi.impl.synthetic; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; import com.intellij.psi.PsiType; import com.intellij.psi.impl.light.LightParameter; import com.intellij.util.IncorrectOperationException; @@ -34,7 +35,7 @@ public class GrLightParameter extends LightParameter implements GrParameter { public static final GrLightParameter[] EMPTY_ARRAY = new GrLightParameter[0]; private volatile boolean myOptional; - public GrLightParameter(@NotNull String name, @NotNull PsiType type, PsiElement scope) { + public GrLightParameter(@NotNull String name, @NotNull PsiType type, @NotNull PsiElement scope) { super(name, type, scope, GroovyFileType.GROOVY_LANGUAGE); } @@ -46,6 +47,16 @@ public class GrLightParameter extends LightParameter implements GrParameter { return null; } + @Override + public PsiElement getContext() { + return getDeclarationScope(); + } + + @Override + public PsiFile getContainingFile() { + return getDeclarationScope().getContainingFile(); + } + public GrLightParameter setOptional(boolean optional) { myOptional = optional; return this; diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java index 0bbd077d5d68..5f0dd84f232a 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java @@ -370,4 +370,9 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase { public void testConstructorWithAllParametersOptional() { doTest(); } + + public void testUnusedDefsForArgs() { + doTest(new UnusedDefInspection()); + } + } \ No newline at end of file diff --git a/plugins/groovy/testdata/highlighting/UnusedDefsForArgs.groovy b/plugins/groovy/testdata/highlighting/UnusedDefsForArgs.groovy new file mode 100644 index 000000000000..6c4a1ae35984 --- /dev/null +++ b/plugins/groovy/testdata/highlighting/UnusedDefsForArgs.groovy @@ -0,0 +1 @@ +args = [] \ No newline at end of file