From f145812a77e5c62ed1813be1eb625c31df49407d Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 3 Mar 2011 19:33:42 +0100 Subject: [PATCH] foreach loop variable shouldn't be visible from the loop expression (IDEA-55859) --- .../daemon/impl/analysis/GenericsHighlightUtil.java | 2 +- .../source/tree/java/PsiForeachStatementImpl.java | 12 +++--------- .../ForeachLoopVariableInIterableExpression.java | 5 +++++ .../advHighlighting/LocalVariableInitialization.java | 2 +- .../completion/SmartTypeCompletionTest.java | 2 ++ 5 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/ForeachLoopVariableInIterableExpression.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java index f0630bbc36d4..d81969add235 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java @@ -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", diff --git a/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiForeachStatementImpl.java b/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiForeachStatementImpl.java index df82f5732ed3..724a3a5a8e36 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiForeachStatementImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiForeachStatementImpl.java @@ -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) { diff --git a/java/java-tests/testData/codeInsight/completion/smartType/ForeachLoopVariableInIterableExpression.java b/java/java-tests/testData/codeInsight/completion/smartType/ForeachLoopVariableInIterableExpression.java new file mode 100644 index 000000000000..a5032b962246 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/ForeachLoopVariableInIterableExpression.java @@ -0,0 +1,5 @@ +public class Bar {{ + for (java.util.List trololo : trolo) { + + } + } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/LocalVariableInitialization.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/LocalVariableInitialization.java index 3c7be47387fe..50ce6dabb281 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/LocalVariableInitialization.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/LocalVariableInitialization.java @@ -181,7 +181,7 @@ public class a { } void forEachParam(java.io.File x) { - for (java.io.File f: f.listFiles()) { + for (java.io.File f: f.listFiles()) { forEachParam(f); } } diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java index bfa4827e4743..8ce191866e02 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java @@ -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");