foreach loop variable shouldn't be visible from the loop expression (IDEA-55859)

This commit is contained in:
peter
2011-03-03 20:07:54 +01:00
parent 588f929005
commit f145812a77
5 changed files with 12 additions and 11 deletions
@@ -701,7 +701,7 @@ public class GenericsHighlightUtil {
public static HighlightInfo checkForeachLoopParameterType(PsiForeachStatement statement) {
final PsiParameter parameter = statement.getIterationParameter();
final PsiExpression expression = statement.getIteratedValue();
if (expression == null) return null;
if (expression == null || expression.getType() == null) return null;
final PsiType itemType = getCollectionItemType(expression);
if (itemType == null) {
String description = JavaErrorMessages.message("foreach.not.applicable",
@@ -22,9 +22,8 @@ import com.intellij.psi.impl.source.Constants;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.impl.source.tree.CompositePsiElement;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.scope.util.PsiScopesUtil;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.ChildRoleBase;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
/**
@@ -126,16 +125,11 @@ public class PsiForeachStatementImpl extends CompositePsiElement implements PsiF
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, this);
if (lastParent == null || lastParent.getParent() != this)
if (lastParent == null || lastParent.getParent() != this || lastParent == getIteratedValue())
// Parent element should not see our vars
return true;
final PsiParameter iterationParameter = getIterationParameter();
if (iterationParameter != null) {
return processor.execute(iterationParameter, state);
}
return PsiScopesUtil.walkChildrenScopes(this, processor, state, lastParent, place);
return processor.execute(getIterationParameter(), state);
}
public void accept(@NotNull PsiElementVisitor visitor) {
@@ -0,0 +1,5 @@
public class Bar {{
for (java.util.List trololo : trolo<caret>) {
}
} }
@@ -181,7 +181,7 @@ public class a {
}
void forEachParam(java.io.File x) {
for (java.io.File f: <error descr="Variable 'f' might not have been initialized">f</error>.listFiles()) {
for (java.io.File f: <error descr="Cannot resolve symbol 'f'">f</error>.listFiles()) {
forEachParam(f);
}
}
@@ -910,6 +910,8 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase {
public void testEmptyListWithCollectionsPrefix() throws Throwable { doItemTest(); }
public void testForeachLoopVariableInIterableExpression() throws Throwable { doAntiTest(); }
public void testStaticallyImportedMagicMethod() throws Throwable {
configureByTestName();
assertStringItems("foo");