diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/YodaCondition.java b/core/src/main/java/com/google/errorprone/bugpatterns/YodaCondition.java index aad89b25e9e..f246fb6f706 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/YodaCondition.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/YodaCondition.java @@ -79,12 +79,12 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state); } if (instanceEqualsInvocation().matches(tree, state)) { - return fix( - tree, - getReceiver(tree), - tree.getArguments().get(0), - /* provideNullSafeFix= */ true, - state); + ExpressionTree receiver = getReceiver(tree); + if (receiver == null) { + // call to equals implicitly qualified by `this` + return NO_MATCH; + } + return fix(tree, receiver, tree.getArguments().get(0), /* provideNullSafeFix= */ true, state); } return NO_MATCH; } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/YodaConditionTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/YodaConditionTest.java index 2cfb93d1f32..a2b545b8a98 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/YodaConditionTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/YodaConditionTest.java @@ -251,4 +251,22 @@ public void nullableYodaCondition() { "}") .doTest(); } + + @Test + public void unqualified() { + testHelper + .addSourceLines( + "Test.java", + "import com.google.common.base.Objects;", + "class Test {", + " @Override", + " public boolean equals(Object other) {", + " return Objects.equal(this, other);", + " }", + " public boolean foo(Object other) {", + " return equals(other);", + " }", + "}") + .doTest(); + } }