From 312d8e46aef97e7514e71ec30f43808baabd322e Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 4 Oct 2024 16:45:29 +0200 Subject: [PATCH] [java] ImplicitSwitchFallThrough should consider switch expressions Fixes #3362 --- docs/pages/release_notes.md | 1 + .../ImplicitSwitchFallThroughRule.java | 28 +++++++++++++------ .../xml/ImplicitSwitchFallThrough.xml | 20 +++++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 197b7a4aec..ea9d2b2f32 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -33,6 +33,7 @@ The old rule names still work but are deprecated. * java * [#4532](https://github.com/pmd/pmd/issues/4532): \[java] Rule misnomer for JUnit* rules * java-errorprone + * [#3362](https://github.com/pmd/pmd/issues/3362): \[java] ImplicitSwitchFallThrough should consider switch expressions * [#5067](https://github.com/pmd/pmd/issues/5067): \[java] CloseResource: False positive for FileSystems.getDefault() ### 🚨 API Changes diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ImplicitSwitchFallThroughRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ImplicitSwitchFallThroughRule.java index 29d91c111c..fb0a9298f5 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ImplicitSwitchFallThroughRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ImplicitSwitchFallThroughRule.java @@ -9,28 +9,40 @@ import java.util.regex.Pattern; import net.sourceforge.pmd.lang.ast.GenericToken; import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; import net.sourceforge.pmd.lang.java.ast.ASTSwitchBranch; +import net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression; import net.sourceforge.pmd.lang.java.ast.ASTSwitchFallthroughBranch; +import net.sourceforge.pmd.lang.java.ast.ASTSwitchLike; import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement; import net.sourceforge.pmd.lang.java.ast.JavaNode; import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule; import net.sourceforge.pmd.lang.java.rule.internal.DataflowPass; import net.sourceforge.pmd.lang.java.rule.internal.DataflowPass.DataflowResult; +import net.sourceforge.pmd.reporting.RuleContext; import net.sourceforge.pmd.util.OptionalBool; public class ImplicitSwitchFallThroughRule extends AbstractJavaRulechainRule { - //todo should consider switch exprs - private static final Pattern IGNORED_COMMENT = Pattern.compile("/[/*].*\\bfalls?[ -]?thr(ough|u)\\b.*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); public ImplicitSwitchFallThroughRule() { - super(ASTSwitchStatement.class); + super(ASTSwitchStatement.class, ASTSwitchExpression.class); } @Override public Object visit(ASTSwitchStatement node, Object data) { + checkSwitchLike(node, asCtx(data)); + return null; + } + + @Override + public Object visit(ASTSwitchExpression node, Object data) { + checkSwitchLike(node, asCtx(data)); + return null; + } + + private void checkSwitchLike(ASTSwitchLike node, RuleContext ruleContext) { DataflowResult dataflow = DataflowPass.getDataflowResult(node.getRoot()); for (ASTSwitchBranch branch : node.getBranches()) { @@ -38,15 +50,14 @@ public class ImplicitSwitchFallThroughRule extends AbstractJavaRulechainRule { ASTSwitchFallthroughBranch fallthrough = (ASTSwitchFallthroughBranch) branch; OptionalBool bool = dataflow.switchBranchFallsThrough(branch); if (bool != OptionalBool.NO - && fallthrough.getStatements().nonEmpty() - && !nextBranchHasComment(branch)) { - asCtx(data).addViolation(branch.getNextBranch().getLabel()); + && fallthrough.getStatements().nonEmpty() + && !nextBranchHasComment(branch)) { + ruleContext.addViolation(branch.getNextBranch().getLabel()); } } else { - return null; + return; } } - return null; } boolean nextBranchHasComment(ASTSwitchBranch branch) { @@ -62,5 +73,4 @@ public class ImplicitSwitchFallThroughRule extends AbstractJavaRulechainRule { } return false; } - } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ImplicitSwitchFallThrough.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ImplicitSwitchFallThrough.xml index 6d86131b0d..0ddd50e522 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ImplicitSwitchFallThrough.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ImplicitSwitchFallThrough.xml @@ -581,4 +581,24 @@ public class Test { } ]]> + + + switch expression with one case, which is not empty + 1 + 6 + +