Skip to content

Commit a116aaf

Browse files
cushonError Prone Team
authored andcommitted
Match on more types in ReferenceEquality
PiperOrigin-RevId: 921575366
1 parent c29ebc0 commit a116aaf

2 files changed

Lines changed: 20 additions & 40 deletions

File tree

core/src/main/java/com/google/errorprone/bugpatterns/ReferenceEquality.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
import com.google.errorprone.BugPattern;
2323
import com.google.errorprone.BugPattern.StandardTags;
2424
import com.google.errorprone.VisitorState;
25-
import com.google.errorprone.suppliers.Supplier;
2625
import com.google.errorprone.util.ASTHelpers;
2726
import com.sun.source.tree.ClassTree;
2827
import com.sun.source.tree.ExpressionTree;
2928
import com.sun.source.tree.LambdaExpressionTree;
3029
import com.sun.source.tree.MethodTree;
31-
import com.sun.tools.javac.code.Scope;
3230
import com.sun.tools.javac.code.Symbol;
3331
import com.sun.tools.javac.code.Symbol.MethodSymbol;
3432
import com.sun.tools.javac.code.Symtab;
3533
import com.sun.tools.javac.code.Type;
36-
import com.sun.tools.javac.util.Name;
3734

3835
/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
3936
@BugPattern(
@@ -65,9 +62,6 @@ protected boolean matchArgument(ExpressionTree tree, VisitorState state) {
6562
if (ASTHelpers.isSubtype(type, state.getSymtab().classType, state)) {
6663
return false;
6764
}
68-
if (!implementsEquals(type, state)) {
69-
return false;
70-
}
7165
return true;
7266
}
7367

@@ -113,28 +107,4 @@ private static boolean overridesMethodOnType(
113107
private static Symbol getOnlyMember(VisitorState state, Type type, String name) {
114108
return getOnlyElement(type.tsym.members().getSymbolsByName(state.getName(name)));
115109
}
116-
117-
/** Check if the method declares or inherits an implementation of .equals() */
118-
public static boolean implementsEquals(Type type, VisitorState state) {
119-
Name equalsName = EQUALS.get(state);
120-
Symbol objectEquals = getOnlyMember(state, state.getSymtab().objectType, "equals");
121-
for (Type sup : state.getTypes().closure(type)) {
122-
if (ASTHelpers.isSameType(sup, state.getSymtab().objectType, state)) {
123-
continue;
124-
}
125-
Scope scope = sup.tsym.members();
126-
if (scope == null) {
127-
continue;
128-
}
129-
for (Symbol sym : scope.getSymbolsByName(equalsName)) {
130-
if (sym.overrides(objectEquals, type.tsym, state.getTypes(), /* checkResult= */ false)) {
131-
return true;
132-
}
133-
}
134-
}
135-
return false;
136-
}
137-
138-
private static final Supplier<Name> EQUALS =
139-
VisitorState.memoize(state -> state.getName("equals"));
140110
}

core/src/test/java/com/google/errorprone/bugpatterns/ReferenceEqualityTest.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void something(TestProtoMessage f1, TestProtoMessage f2) {
5454
}
5555

5656
@Test
57-
public void negative_const() {
57+
public void positive_const() {
5858
compilationHelper
5959
.addSourceLines(
6060
"Foo.java",
@@ -70,10 +70,12 @@ class Test {
7070
public static final Foo CONST = new Foo();
7171
7272
boolean f(Foo a) {
73+
// BUG: Diagnostic contains:
7374
return a == CONST;
7475
}
7576
7677
boolean f(Object o, Foo a) {
78+
// BUG: Diagnostic contains:
7779
return o == a;
7880
}
7981
}
@@ -82,7 +84,7 @@ boolean f(Object o, Foo a) {
8284
}
8385

8486
@Test
85-
public void negative_extends_equalsObject() {
87+
public void extends_equalsObject() {
8688
compilationHelper
8789
.addSourceLines(
8890
"Sup.java",
@@ -100,6 +102,7 @@ public boolean equals(Object o) {
100102
101103
class Test extends Sup {
102104
boolean f(Object a, Test b) {
105+
// BUG: Diagnostic contains: a.equals(b)
103106
return a == b;
104107
}
105108
}
@@ -158,7 +161,7 @@ boolean f(Test a, Test b) {
158161
}
159162

160163
@Test
161-
public void negative_noEquals() {
164+
public void positive_noEquals() {
162165
compilationHelper
163166
.addSourceLines(
164167
"Test.java",
@@ -167,6 +170,7 @@ public void negative_noEquals() {
167170
168171
class Test {
169172
boolean f(Test a, Test b) {
173+
// BUG: Diagnostic contains: a.equals(b)
170174
return a == b;
171175
}
172176
}
@@ -363,6 +367,7 @@ interface Sup {
363367
364368
class Test implements Sup {
365369
boolean f(Object a, Test b) {
370+
// BUG: Diagnostic contains: a.equals(b)
366371
return a == b;
367372
}
368373
}
@@ -434,13 +439,17 @@ public void erroneous() {
434439
compilationHelper
435440
.addSourceLines(
436441
"Test.java",
437-
"import " + MayImplementEquals.class.getCanonicalName() + ";",
438-
"abstract class Test {",
439-
" abstract MayImplementEquals getter();",
440-
" boolean f(MayImplementEquals b) {",
441-
" return getter() == b;",
442-
" }",
443-
"}")
442+
"""
443+
import %s;
444+
abstract class Test {
445+
abstract MayImplementEquals getter();
446+
boolean f(MayImplementEquals b) {
447+
// BUG: Diagnostic contains: getter().equals(b)
448+
return getter() == b;
449+
}
450+
}
451+
"""
452+
.formatted(MayImplementEquals.class.getCanonicalName()))
444453
.withClasspath(MayImplementEquals.class, ReferenceEqualityTest.class)
445454
.doTest();
446455
}
@@ -463,6 +472,7 @@ boolean g(T t1, T t2) {
463472
}
464473
465474
boolean g(X x1, X x2) {
475+
// BUG: Diagnostic contains:
466476
return x1 == x2;
467477
}
468478
}

0 commit comments

Comments
 (0)