diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java index 1e3bb608c47b..f95d6aef1b42 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java @@ -409,12 +409,15 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo } static InitializerUsageStatus getInitializerUsageStatus(PsiVariable var, PsiStatement nextStatement) { - if(var.getInitializer() == null) return UNKNOWN; + if(!(var instanceof PsiLocalVariable) || var.getInitializer() == null) return UNKNOWN; if(isDeclarationJustBefore(var, nextStatement)) return DECLARED_JUST_BEFORE; PsiElement declaration = var.getParent(); // Check if variable is not referenced in the same declaration like "int a = 0, b = a;" if(!PsiTreeUtil.processElements(declaration, e -> !(e instanceof PsiReferenceExpression) || ((PsiReferenceExpression)e).resolve() != var)) return UNKNOWN; + // Check that variable is declared in the same method or the same lambda expression + if(PsiTreeUtil.getParentOfType(var, PsiLambdaExpression.class, PsiMethod.class) != + PsiTreeUtil.getParentOfType(nextStatement, PsiLambdaExpression.class, PsiMethod.class)) return UNKNOWN; PsiElement block = PsiUtil.getVariableCodeBlock(var, null); if(block == null) return UNKNOWN; final ControlFlow controlFlow; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectWithSameLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectWithSameLambda.java new file mode 100644 index 000000000000..d462fc566c5e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectWithSameLambda.java @@ -0,0 +1,15 @@ +// "Replace with collect" "true" +import java.util.*; +import java.util.stream.Collectors; + +public class Collect { + public static void collectWithLambda(List test) { + Runnable r = () -> { + List result; + System.out.println("We're inside the lambda"); + result = test.stream().map(String::trim).collect(Collectors.toList()); + System.out.println(result); + }; + r.run(); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectWithAnonymous.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectWithAnonymous.java new file mode 100644 index 000000000000..b9d865aed9b5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectWithAnonymous.java @@ -0,0 +1,18 @@ +// "Replace with collect" "false" +import java.util.*; + +public class Collect { + public static void collectWithAnonymous(List test) { + List result = new ArrayList<>(); + Runnable r = new Runnable() { + @Override + public void run() { + for (String str : test) { + result.add(str.trim()); + } + } + }; + r.run(); + System.out.println(result); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectWithLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectWithLambda.java new file mode 100644 index 000000000000..9ab7b567012d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectWithLambda.java @@ -0,0 +1,15 @@ +// "Replace with collect" "false" +import java.util.*; + +public class Collect { + public static void collectWithLambda(List test) { + List result = new ArrayList<>(); + Runnable r = () -> { + for(String str : test) { + result.add(str.trim()); + } + }; + r.run(); + System.out.println(result); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectWithSameLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectWithSameLambda.java new file mode 100644 index 000000000000..5162e6dfeea9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectWithSameLambda.java @@ -0,0 +1,16 @@ +// "Replace with collect" "true" +import java.util.*; + +public class Collect { + public static void collectWithLambda(List test) { + Runnable r = () -> { + List result = new ArrayList<>(); + System.out.println("We're inside the lambda"); + for(String str : test) { + result.add(str.trim()); + } + System.out.println(result); + }; + r.run(); + } +}