diff --git a/.ci/files/all-java.xml b/.ci/files/all-java.xml index 0a86a06f54..41c5d8ead0 100644 --- a/.ci/files/all-java.xml +++ b/.ci/files/all-java.xml @@ -258,7 +258,7 @@ - + diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/UnnecessaryConversionTemporaryRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/UnnecessaryConversionTemporaryRule.java deleted file mode 100644 index 8b5d74d6d6..0000000000 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/UnnecessaryConversionTemporaryRule.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.lang.java.rule.errorprone; - -import static net.sourceforge.pmd.util.CollectionUtil.setOf; - -import java.util.Set; - -import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; -import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression; -import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix; -import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; - -public class UnnecessaryConversionTemporaryRule extends AbstractJavaRule { - - private boolean inPrimaryExpressionContext; - private ASTPrimaryExpression primary; - private boolean usingPrimitiveWrapperAllocation; - - private static final Set PRIMITIVE_WRAPPERS = setOf("Integer", "Boolean", "Double", "Long", "Short", "Byte", "Float"); - - @Override - public Object visit(ASTPrimaryExpression node, Object data) { - if (node.getNumChildren() == 0 || node.getChild(0).getNumChildren() == 0 - || !(node.getChild(0).getChild(0) instanceof ASTAllocationExpression)) { - return super.visit(node, data); - } - // TODO... hmmm... is this inPrimaryExpressionContext gibberish - // necessary? - inPrimaryExpressionContext = true; - primary = node; - super.visit(node, data); - inPrimaryExpressionContext = false; - usingPrimitiveWrapperAllocation = false; - return data; - } - - @Override - public Object visit(ASTAllocationExpression node, Object data) { - if (!inPrimaryExpressionContext || !(node.getChild(0) instanceof ASTClassOrInterfaceType)) { - return super.visit(node, data); - } - if (!PRIMITIVE_WRAPPERS.contains(node.getChild(0).getImage())) { - return super.visit(node, data); - } - usingPrimitiveWrapperAllocation = true; - return super.visit(node, data); - } - - @Override - public Object visit(ASTPrimarySuffix node, Object data) { - if (inPrimaryExpressionContext && usingPrimitiveWrapperAllocation) { - if (node.hasImageEqualTo("toString")) { - if (node.getParent() == primary) { - addViolation(data, node); - } - } - } - return super.visit(node, data); - } - -} diff --git a/pmd-java/src/main/resources/category/java/errorprone.xml b/pmd-java/src/main/resources/category/java/errorprone.xml index 1849bd2e26..6e005e91ea 100644 --- a/pmd-java/src/main/resources/category/java/errorprone.xml +++ b/pmd-java/src/main/resources/category/java/errorprone.xml @@ -3163,13 +3163,32 @@ boolean answer2 = buz.toUpperCase().equalsIgnoreCase("baz"); // another unnec language="java" since="0.1" message="Avoid unnecessary temporaries when converting primitives to Strings" - class="net.sourceforge.pmd.lang.java.rule.errorprone.UnnecessaryConversionTemporaryRule" + class="net.sourceforge.pmd.lang.rule.XPathRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#unnecessaryconversiontemporary"> Avoid the use temporary objects when converting primitives to Strings. Use the static conversion methods on the wrapper classes instead. 3 + + + + + + + + + + + Preferred approach - static toString + 0 +