From f1d46714a5c8f7a9113ab84819044283b278db50 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 18 Jun 2020 12:10:36 +0200 Subject: [PATCH] [java] InefficientStringBuffering: fix another ternary false positive --- .../java/rule/performance/InefficientStringBufferingRule.java | 2 +- .../java/rule/performance/xml/InefficientStringBuffering.xml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientStringBufferingRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientStringBufferingRule.java index a2c298095a..3850e21101 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientStringBufferingRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientStringBufferingRule.java @@ -46,7 +46,7 @@ public class InefficientStringBufferingRule extends AbstractJavaRule { @Override public Object visit(ASTAdditiveExpression node, Object data) { - if (node.getParent() instanceof ASTConditionalExpression) { + if (node.getParent() instanceof ASTConditionalExpression || node.getNthParent(2) instanceof ASTConditionalExpression) { // ignore concats in ternary expressions return data; } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InefficientStringBuffering.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InefficientStringBuffering.xml index 9533ad0279..0c31fbb10a 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InefficientStringBuffering.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InefficientStringBuffering.xml @@ -447,6 +447,7 @@ public class Foo { public String toString() { StringBuilder sb = new StringBuilder("Foo{"); sb.append("someInt=").append(this.someInt < 0 ? "n/a" : this.someInt + "ms"); + sb.append("someInt2=").append(this.someInt >= 0 ? this.someInt + "ms" : "n/a"); sb.append('}'); return sb.toString(); }