Skip to content

Commit

Permalink
Merge branch 'pr-85' into pmd/5.4.x
Browse files Browse the repository at this point in the history
Closes #85
Note: pr-85 has been rebased onto pmd/5.3.x
  • Loading branch information
adangel committed Feb 28, 2016
2 parents 15a82f0 + fc35a16 commit a81de95
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Expand Up @@ -6,6 +6,8 @@
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTArgumentList;
import net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator;
import net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression;
import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
Expand Down Expand Up @@ -37,6 +39,16 @@ public Object visit(ASTVariableDeclaratorId node, Object data) {
// used in method call
continue;
}
ASTEqualityExpression equality = name.getFirstParentOfType(ASTEqualityExpression.class);
if (equality != null && equality.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// used in condition
continue;
}
ASTConditionalExpression conditional = name.getFirstParentOfType(ASTConditionalExpression.class);
if (conditional != null && name.jjtGetParent().jjtGetParent().jjtGetParent() == conditional && conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// is used in ternary as only option (not appendend to other string)
continue;
}
if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);
if(astName != null){
Expand Down
Expand Up @@ -135,15 +135,32 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description>#1340 UseStringBufferForStringAppends False Positive with ternary operator</description>
<description>#1340 UseStringBufferForStringAppends False Positive with ternary operator (used in condition) </description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class UseStringBuffer {
public void foo() {
String country = (country == null || "".equals(country))
String value = "";
value = (value != null) ? "1" : "0";
}
}
]]></code>
</test-code>
<test-code>
<description>#1340 UseStringBufferForStringAppends False Positive with ternary operator (used both in condition and options)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class UseStringBuffer {
public void foo() {
String country = "";
country = (country == null || "".equals(country))
? ((String) getCountry())
: country;
}
public void foo2() {
String country = request.getParameter("country");
country = (country == null) ? "USA" : country;
}
}
]]></code>
</test-code>
Expand Down
3 changes: 3 additions & 0 deletions src/site/markdown/overview/changelog.md
Expand Up @@ -23,6 +23,7 @@
* [#79](https://github.com/pmd/pmd/pull/79): do not flag public static void main(String[]) as UseVarargs; ignore @Override for UseVarargs
* [#80](https://github.com/pmd/pmd/pull/80): Update mvn-plugin.md
* [#83](https://github.com/pmd/pmd/pull/83): Adds new Code Climate-compliant JSON renderer
* [#85](https://github.com/pmd/pmd/pull/85): #1340 UseStringBufferForStringAppends False Positive with Ternary Operator

**Bugfixes:**

Expand All @@ -38,6 +39,8 @@
* [#1465](https://sourceforge.net/p/pmd/bugs/1465/): False Positve UnusedImports with javadoc @link
* java-junit/TestClassWithoutTestCases:
* [#1453](https://sourceforge.net/p/pmd/bugs/1453/): Test Class Without Test Cases gives false positive
* java-optimizations/UseStringBufferForStringAppends:
* [#1340](https://sourceforge.net/p/pmd/bugs/1340/): UseStringBufferForStringAppends False Positive with ternary operator
* java-unusedcode/UnusedFormalParameter:
* [#1456](https://sourceforge.net/p/pmd/bugs/1456/): UnusedFormalParameter should ignore overriding methods
* General
Expand Down

0 comments on commit a81de95

Please sign in to comment.