diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/AnonymousCanBeLambdaInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/AnonymousCanBeLambdaInspection.java index caffccb738d1..12b1d6cdfa2d 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/AnonymousCanBeLambdaInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/AnonymousCanBeLambdaInspection.java @@ -271,7 +271,7 @@ public class AnonymousCanBeLambdaInspection extends AbstractBaseJavaLocalInspect final PsiCodeBlock body = method.getBody(); LOG.assertTrue(body != null); - final Collection comments = collectCommentsOutsideMethodBody(anonymousClass, body); + final Collection comments = collectCommentsOutsideMethodBody(anonymousClass.getParent(), body); final Project project = anonymousClass.getProject(); final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); @@ -326,7 +326,7 @@ public class AnonymousCanBeLambdaInspection extends AbstractBaseJavaLocalInspect } @NotNull - static Collection collectCommentsOutsideMethodBody(PsiAnonymousClass anonymousClass, PsiCodeBlock body) { + static Collection collectCommentsOutsideMethodBody(PsiElement anonymousClass, PsiCodeBlock body) { final Collection psiComments = PsiTreeUtil.findChildrenOfType(anonymousClass, PsiComment.class); psiComments.removeIf(comment -> PsiTreeUtil.isAncestor(body, comment, false)); return ContainerUtil.map(psiComments, (comment) -> (PsiComment)comment.copy()); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/afterCommentsInsideAnonymousClassBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/afterCommentsInsideAnonymousClassBody.java index 64800159149d..ac0864c8ac21 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/afterCommentsInsideAnonymousClassBody.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/afterCommentsInsideAnonymousClassBody.java @@ -1,6 +1,7 @@ // "Replace with lambda" "true" class Test { - //my comment + //comment inside new expression +//my comment Runnable r = () -> {}; } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/beforeCommentsInsideAnonymousClassBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/beforeCommentsInsideAnonymousClassBody.java index 304804c3853f..f9e03bedeafb 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/beforeCommentsInsideAnonymousClassBody.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/beforeCommentsInsideAnonymousClassBody.java @@ -1,7 +1,8 @@ // "Replace with lambda" "true" class Test { - Runnable r = new Runnable() { + Runnable r = new //comment inside new expression + Runnable() { //my comment public void run () {} };