From c8dc243d717b60662c13b8b95d9767155bc7a74b Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Wed, 7 Nov 2018 17:55:16 +0100 Subject: [PATCH] redundant suppression (fall through switch stmt): ensure suppressed will be reported by inspection (IDEA-201885) --- .../beforeFallThroughSuppression.java | 96 +++++++++++++++++++ .../RemoveRedundantSuppressionTest.java | 2 + .../FallthruInSwitchStatementInspection.java | 3 +- 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantUncheckedVarargs/beforeFallThroughSuppression.java diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantUncheckedVarargs/beforeFallThroughSuppression.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantUncheckedVarargs/beforeFallThroughSuppression.java new file mode 100644 index 000000000000..9019f8fd5c35 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantUncheckedVarargs/beforeFallThroughSuppression.java @@ -0,0 +1,96 @@ +// "Remove 'fallthrough' suppression" "false" +class MyTest { + + private static void unescapeStringCharacters(int length, @NotNull String s, @NotNull StringBuilder buffer) { + boolean escaped = false; + for (int idx = 0; idx < length; idx++) { + char ch = s.charAt(idx); + if (!escaped) { + if (ch == '\\') { + escaped = true; + } else { + buffer.append(ch); + } + } else { + int octalEscapeMaxLength = 2; + switch (ch) { + case 'n': + buffer.append('\n'); + break; + + case 'r': + buffer.append('\r'); + break; + + case 'b': + buffer.append('\b'); + break; + + case 't': + buffer.append('\t'); + break; + + case 'f': + buffer.append('\f'); + break; + + case '\'': + buffer.append('\''); + break; + + case '\"': + buffer.append('\"'); + break; + + case '\\': + buffer.append('\\'); + break; + + case 'u': + if (idx + 4 < length) { + try { + int code = Integer.parseInt(s.substring(idx + 1, idx + 5), 16); + //noinspection AssignmentToForLoopParameter + idx += 4; + buffer.append((char) code); + } catch (NumberFormatException e) { + buffer.append("\\u"); + } + } else { + buffer.append("\\u"); + } + break; + + case '0': + case '1': + case '2': + case '3': + octalEscapeMaxLength = 3; + //noinspection fallthrough + case '4': + case '5': + case '6': + case '7': + int escapeEnd = idx + 1; + while (escapeEnd < length && escapeEnd < idx + octalEscapeMaxLength) + escapeEnd++; + try { + buffer.append((char) Integer.parseInt(s.substring(idx, escapeEnd), 8)); + } catch (NumberFormatException e) { + throw new RuntimeException("Couldn't parse " + s.substring(idx, escapeEnd), e); // shouldn't happen + } + //noinspection AssignmentToForLoopParameter + idx = escapeEnd - 1; + break; + + default: + buffer.append(ch); + break; + } + escaped = false; + } + } + + if (escaped) buffer.append('\\'); + } +} \ No newline at end of file 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 6401aecfe87d..837d5e873142 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 @@ -22,6 +22,7 @@ import com.intellij.codeInspection.RedundantSuppressInspection; import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection; import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection; import com.intellij.psi.impl.source.tree.injected.MyTestInjector; +import com.siyeh.ig.controlflow.FallthruInSwitchStatementInspection; public class RemoveRedundantSuppressionTest extends LightQuickFixParameterizedTestCase { @@ -33,6 +34,7 @@ public class RemoveRedundantSuppressionTest extends LightQuickFixParameterizedTe enableInspectionTools(new RedundantSuppressInspection(), new PossibleHeapPollutionVarargsInspection(), new UncheckedWarningLocalInspection(), + new FallthruInSwitchStatementInspection(), new UnusedDeclarationInspection(true), new RedundantLambdaCodeBlockInspection()); } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspection.java index 9540eab8eefc..b8cf63bfdd08 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspection.java @@ -15,6 +15,7 @@ */ package com.siyeh.ig.controlflow; +import com.intellij.codeInspection.JavaSuppressionUtil; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; import com.intellij.psi.*; @@ -100,7 +101,7 @@ public class FallthruInSwitchStatementInspection extends BaseInspection { if (previousSibling instanceof PsiComment) { final PsiComment comment = (PsiComment)previousSibling; final String commentText = comment.getText(); - if (commentPattern.matcher(commentText).find()) { + if (commentPattern.matcher(commentText).find() && JavaSuppressionUtil.getSuppressedInspectionIdsIn(comment) == null) { continue; } }