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 03436dca4b..1238a746d1 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 @@ -187,7 +187,7 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRulechainRule { counter.reset(); } else if (invocation.getArguments().getFirstChild() instanceof ASTStringLiteral || invocation instanceof ASTMethodCall) { - counter.count(invocation); + counter.count(invocation.getArguments().getFirstChild()); } } @@ -198,9 +198,10 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRulechainRule { checkForViolation(data); if (firstArg instanceof ASTInfixExpression) { - if (((ASTInfixExpression) firstArg).getRightOperand() instanceof ASTStringLiteral) { + ASTExpression rightOperand = ((ASTInfixExpression) firstArg).getRightOperand(); + if (rightOperand instanceof ASTStringLiteral) { // argument ends with ... + "some string" - counter.count(invocation); + counter.count(rightOperand); } } else { // continue with a fresh round @@ -209,7 +210,7 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRulechainRule { } else { // no variables appended, compiler will take care of merging all the // string concats, we really only have 1 then - counter.count(invocation); + counter.count(invocation.getArguments().getFirstChild()); } } 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 6052782aeb..f59df95de6 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 @@ -953,7 +953,7 @@ public class Foo { 32, Including the constructor's string 4 - 3,8,15,26 + 3,8,15,27 + + + + Wrong count of appends - should be 3 + 2 + 4,8 + + StringBuffer (or StringBuilder).append is called 3 consecutive times with literals. Use a single append with a single combined String. + StringBuffer (or StringBuilder).append is called 2 consecutive times with literals. Use a single append with a single combined String. + +