EA-26380 - assert: UnusedDefInspection.isUsedInToplevelFlowOnly

This commit is contained in:
Maxim Medvedev
2011-04-18 15:31:31 +04:00
parent 3df5de3a01
commit e4d9382205
5 changed files with 27 additions and 16 deletions
@@ -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<PsiReference>() {
@@ -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() {
@@ -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");
@@ -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;
@@ -370,4 +370,9 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
public void testConstructorWithAllParametersOptional() {
doTest();
}
public void testUnusedDefsForArgs() {
doTest(new UnusedDefInspection());
}
}
@@ -0,0 +1 @@
<warning descr="Assignment is not used">args</warning> = []