Merge branch 'pr-307'

This commit is contained in:
Juan Martín Sotuyo Dodero
2017-03-23 13:28:09 -03:00
3 changed files with 25 additions and 6 deletions

View File

@ -19,7 +19,7 @@ import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
public class UseStringBufferForStringAppendsRule extends AbstractJavaRule {
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeHelper.isA(node, String.class) || node.isArray()) {
@ -46,11 +46,15 @@ public class UseStringBufferForStringAppendsRule extends AbstractJavaRule {
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 (conditional != null) {
Node thirdParent = name.jjtGetParent().jjtGetParent().jjtGetParent();
if ((thirdParent == conditional || thirdParent.jjtGetParent() == conditional)
&& conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// is used in ternary as only option (not appended to other
// string)
continue;
}
}
if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);

View File

@ -161,6 +161,18 @@ public class UseStringBuffer {
String country = request.getParameter("country");
country = (country == null) ? "USA" : country;
}
}
]]></code>
</test-code>
<test-code>
<description>#222 False positive when inverting ternary expression arguments</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public void bar() {
String country = request.getParameter("country");
country = (country != null) ? country : "USA";
}
}
]]></code>
</test-code>