From 7f299c4f6c3aac04fe0b6ae91f1d8afef40cc536 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 4 Dec 2020 18:21:11 +0100 Subject: [PATCH] [java] CompareObjectsWithEquals: only allow this with equals (#2934) --- pmd-java/src/main/resources/category/java/errorprone.xml | 5 ++++- .../rule/errorprone/xml/CompareObjectsWithEquals.xml | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pmd-java/src/main/resources/category/java/errorprone.xml b/pmd-java/src/main/resources/category/java/errorprone.xml index 2e02d7e2f3..ae48bb363b 100644 --- a/pmd-java/src/main/resources/category/java/errorprone.xml +++ b/pmd-java/src/main/resources/category/java/errorprone.xml @@ -1114,7 +1114,10 @@ Use equals() to compare object references; avoid comparing them with ==. [not(pmd-java:typeIs('java.lang.Enum'))] [not(pmd-java:typeIs('java.lang.Class'))]) = 2 ] - [not(PrimaryExpression[PrimaryPrefix/@ThisModifier = true()][not(PrimarySuffix)])] + [not(PrimaryExpression[PrimaryPrefix/@ThisModifier = true()] + [not(PrimarySuffix)] + [ancestor::MethodDeclaration[@Name = 'equals']]) + ] ]]> diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CompareObjectsWithEquals.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CompareObjectsWithEquals.xml index 3a4b2dc453..893433e2e4 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CompareObjectsWithEquals.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CompareObjectsWithEquals.xml @@ -294,7 +294,8 @@ public class ClassWithFields { #2934 this and class should be ignored - 0 + 1 + 16