From 01eb8ca66ba3a75ede9a256323e6704f89eaf001 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Wed, 26 Sep 2018 17:47:16 +0200 Subject: [PATCH] redundant lambda code block: warn for self suppressions so it would be suppressed by LocalInspectionPass otherwise Redundant Suppression would report such cases as suppress comment would prevent reporting by itself --- .../RedundantLambdaCodeBlockInspection.java | 16 ++++++++++++++-- .../beforeRedundandLambdaBlock.java | 19 +++++++++++++++++++ .../RemoveRedundantSuppressionTest.java | 4 +++- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantUncheckedVarargs/beforeRedundandLambdaBlock.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/RedundantLambdaCodeBlockInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/RedundantLambdaCodeBlockInspection.java index 71ce64a0483e..b065bb5baf73 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/RedundantLambdaCodeBlockInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/RedundantLambdaCodeBlockInspection.java @@ -15,6 +15,7 @@ import java.util.Collection; public class RedundantLambdaCodeBlockInspection extends AbstractBaseJavaLocalInspectionTool { public static final Logger LOG = Logger.getInstance(RedundantLambdaCodeBlockInspection.class); + private static final String SHORT_NAME = "CodeBlock2Expr"; @Nls @NotNull @@ -38,7 +39,7 @@ public class RedundantLambdaCodeBlockInspection extends AbstractBaseJavaLocalIns @NotNull @Override public String getShortName() { - return "CodeBlock2Expr"; + return SHORT_NAME; } @NotNull @@ -84,7 +85,18 @@ public class RedundantLambdaCodeBlockInspection extends AbstractBaseJavaLocalIns private static boolean findCommentsOutsideExpression(PsiElement body, PsiExpression psiExpression) { final Collection comments = PsiTreeUtil.findChildrenOfType(body, PsiComment.class); for (PsiComment comment : comments) { - if (!PsiTreeUtil.isAncestor(psiExpression, comment, true)) { + if (!PsiTreeUtil.isAncestor(psiExpression, comment, true) && !isSelfSuppressionComment(comment)) { + return true; + } + } + return false; + } + + private static boolean isSelfSuppressionComment(PsiComment comment) { + String suppressString = JavaSuppressionUtil.getSuppressedInspectionIdsIn(comment); + if (suppressString != null) { + String[] suppressIds = suppressString.split(","); + if (suppressIds.length == 1 && SHORT_NAME.equals(suppressIds[0])) { return true; } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantUncheckedVarargs/beforeRedundandLambdaBlock.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantUncheckedVarargs/beforeRedundandLambdaBlock.java new file mode 100644 index 000000000000..904060303e46 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantUncheckedVarargs/beforeRedundandLambdaBlock.java @@ -0,0 +1,19 @@ +// "Remove 'CodeBlock2Expr' suppression" "false" +import java.util.*; + +interface I { + int m(); +} +public class SampleSafeVarargs { + + { + I i = () -> { + //noinspection CodeBlock2Expr + return foo(); + }; + } + + int foo() { + return 1; + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/RemoveRedundantSuppressionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/RemoveRedundantSuppressionTest.java index 22c2b8d55a96..27ef80bae24d 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/RemoveRedundantSuppressionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/RemoveRedundantSuppressionTest.java @@ -17,6 +17,7 @@ package com.intellij.java.codeInsight.daemon.quickFix; import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; import com.intellij.codeInspection.PossibleHeapPollutionVarargsInspection; +import com.intellij.codeInspection.RedundantLambdaCodeBlockInspection; import com.intellij.codeInspection.RedundantSuppressInspection; import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection; import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection; @@ -30,7 +31,8 @@ public class RemoveRedundantSuppressionTest extends LightQuickFixParameterizedTe enableInspectionTools(new RedundantSuppressInspection(), new PossibleHeapPollutionVarargsInspection(), new UncheckedWarningLocalInspection(), - new UnusedDeclarationInspection(true)); + new UnusedDeclarationInspection(true), + new RedundantLambdaCodeBlockInspection()); } @Override