Turn SuspiciousHashcodeMethodName into xpath rule

This commit is contained in:
Clément Fournier committed 2021-07-07 16:33:06 +02:00
1 parent 6f36b20d87
commit 97af85eda4
2 files changed
+15 -12

No files matched your search

@@ -57,7 +57,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTNullLiteral;
import net.sourceforge.pmd.lang.java.ast.ASTNumericLiteral;
import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType;
import net.sourceforge.pmd.lang.java.ast.ASTStatement;
import net.sourceforge.pmd.lang.java.ast.ASTSuperExpression;
import net.sourceforge.pmd.lang.java.ast.ASTThisExpression;
@@ -178,16 +177,6 @@ public final class JavaRuleUtil {
return false;
}
/**
* Returns true if this is a primitive type node with the given kind.
*/
public static boolean isPrimitiveType(JavaNode node, PrimitiveTypeKind kind) {
if (node instanceof ASTPrimitiveType) {
return ((ASTPrimitiveType) node).getKind() == kind;
}
return false;
}
/** This is type-aware, so will not pick up on numeric addition. */
public static boolean isStringConcatExpr(@Nullable JavaNode e) {
if (e instanceof ASTInfixExpression) {
@@ -3015,12 +3015,26 @@ public class Foo {
language="java"
since="1.5"
message="The method name and return type are suspiciously close to hashCode()"
class="net.sourceforge.pmd.lang.java.rule.errorprone.SuspiciousHashcodeMethodNameRule"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#suspicioushashcodemethodname">
<description>
The method name and return type are suspiciously close to hashCode(), which may denote an intention
to override the hashCode() method.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//MethodDeclaration[
lower-case(@Name) = 'hashcode'
and @Name != 'hashCode'
and @Arity = 0
and PrimitiveType[@Kind = 'int']
]
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[