IDEA-121658 Java 1.8: "Replace with forEach" quick fix is not suggested if for statement is not surrounded with braces

This commit is contained in:
Anna Kozlova
2014-03-05 11:39:47 +01:00
parent c692398423
commit 0fa7d08f83
4 changed files with 28 additions and 2 deletions
@@ -223,6 +223,12 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
else if (body instanceof PsiBlockStatement) {
return extractMethodCallFromBlock(((PsiBlockStatement)body).getCodeBlock());
}
else if (body instanceof PsiExpressionStatement) {
final PsiExpression expression = ((PsiExpressionStatement)body).getExpression();
if (expression instanceof PsiCallExpression) {
methodCall = (PsiCallExpression)expression;
}
}
return methodCall;
}
@@ -111,10 +111,10 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
if (effectivelyFinal[0]) {
if (isCollectCall(body)) {
holder.registerProblem(iteratedValue, "Can be replaced with collect call",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithCollectCallFix());
ProblemHighlightType.LIKE_UNUSED_SYMBOL, new ReplaceWithCollectCallFix());
} else if (!isTrivial(body, statement.getIterationParameter(), iteratedValueType)) {
holder.registerProblem(iteratedValue, "Can be replaced with foreach call",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithForeachCallFix());
ProblemHighlightType.LIKE_UNUSED_SYMBOL, new ReplaceWithForeachCallFix());
}
}
}
@@ -0,0 +1,10 @@
// "Replace with forEach" "true"
import java.util.ArrayList;
import java.util.List;
class Sample {
List<String> foo = new ArrayList<>();
{
foo.forEach(System.out::println);
}
}
@@ -0,0 +1,10 @@
// "Replace with forEach" "true"
import java.util.ArrayList;
import java.util.List;
class Sample {
List<String> foo = new ArrayList<>();
{
for (String s : fo<caret>o) System.out.println(s);
}
}