diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index b8f8783555..9190144388 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -16,6 +16,9 @@ This is a {{ site.pmd.release_type }} release. ### Fixed Issues +* java-performance + * [#3152](https://github.com/pmd/pmd/issues/3152): \[java] ConsecutiveLiteralAppends and InsufficientStringBufferDeclaration: FP with switch expressions + ### API Changes ### External Contributions diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveLiteralAppendsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveLiteralAppendsRule.java index 58bafe8fa9..2323b5265d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveLiteralAppendsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveLiteralAppendsRule.java @@ -28,6 +28,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTName; import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression; import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix; import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel; +import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabeledBlock; +import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabeledExpression; import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer; @@ -80,6 +82,8 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRule { BLOCK_PARENTS.add(ASTCatchStatement.class); BLOCK_PARENTS.add(ASTFinallyStatement.class); BLOCK_PARENTS.add(ASTLambdaExpression.class); + BLOCK_PARENTS.add(ASTSwitchLabeledBlock.class); + BLOCK_PARENTS.add(ASTSwitchLabeledExpression.class); } private static final PropertyDescriptor THRESHOLD_DESCRIPTOR @@ -91,6 +95,7 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRule { public ConsecutiveLiteralAppendsRule() { definePropertyDescriptor(THRESHOLD_DESCRIPTOR); + addRuleChainVisit(ASTVariableDeclaratorId.class); } @Override diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InsufficientStringBufferDeclarationRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InsufficientStringBufferDeclarationRule.java index 86bc9b6c99..e2fe4e7bd5 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InsufficientStringBufferDeclarationRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InsufficientStringBufferDeclarationRule.java @@ -25,6 +25,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression; import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix; import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix; import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel; +import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabeledBlock; +import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabeledExpression; import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; @@ -44,14 +46,20 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule { private static final Set> BLOCK_PARENTS; static { - BLOCK_PARENTS = new HashSet<>(2); + BLOCK_PARENTS = new HashSet<>(); BLOCK_PARENTS.add(ASTIfStatement.class); BLOCK_PARENTS.add(ASTSwitchStatement.class); + BLOCK_PARENTS.add(ASTSwitchLabeledBlock.class); + BLOCK_PARENTS.add(ASTSwitchLabeledExpression.class); } // as specified in StringBuffer and StringBuilder public static final int DEFAULT_BUFFER_SIZE = 16; + public InsufficientStringBufferDeclarationRule() { + addRuleChainVisit(ASTVariableDeclaratorId.class); + } + @Override public Object visit(ASTVariableDeclaratorId node, Object data) { if (node.getNameDeclaration() == null diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/ConsecutiveLiteralAppends.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/ConsecutiveLiteralAppends.xml index c72fd4f721..5d5e3acd39 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/ConsecutiveLiteralAppends.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/ConsecutiveLiteralAppends.xml @@ -1497,4 +1497,37 @@ public class Foo { } ]]> + + [java] ConsecutiveLiteralAppends and InsufficientStringBufferDeclaration: FP with switch expressions #3152 + 0 + { builder.append("<"); } // Rule:ConsecutiveLiteralAppends Priority:3 StringBuffer (or StringBuilder).append is called 6 consecutive times with literals. Use a single append with a single combined String.. + case '>' -> builder.append(">"); + case '"' -> builder.append("""); + case '&' -> builder.append("&"); + case '\'' -> builder.append("'"); + case '/' -> builder.append("/"); + default -> builder.append(ch); + } + } + return builder.toString(); + } + + private static int findHTMLReservedChar(String text) { + return 0; + } +} + ]]> + diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InsufficientStringBufferDeclaration.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InsufficientStringBufferDeclaration.xml index 859047f8f4..39335c141a 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InsufficientStringBufferDeclaration.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InsufficientStringBufferDeclaration.xml @@ -1068,4 +1068,37 @@ public class Test { } ]]> + + [java] ConsecutiveLiteralAppends and InsufficientStringBufferDeclaration: FP with switch expressions #3152 + 0 + { builder.append("<"); } // Rule:ConsecutiveLiteralAppends Priority:3 StringBuffer (or StringBuilder).append is called 6 consecutive times with literals. Use a single append with a single combined String.. + case '>' -> builder.append(">"); + case '"' -> builder.append("""); + case '&' -> builder.append("&"); + case '\'' -> builder.append("'"); + case '/' -> builder.append("/"); + default -> builder.append(ch); + } + } + return builder.toString(); + } + + private static int findHTMLReservedChar(String text) { + return 0; + } +} + ]]> +