Skip to content

Commit

Permalink
ReturnValueIgnored: include methods defined in java.lang.Object.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 375914006
  • Loading branch information
graememorgan authored and Error Prone Team committed May 26, 2021
1 parent 0a6f515 commit e9aa66a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ private static boolean functionalMethod(ExpressionTree tree, VisitorState state)
.onDescendantOf(PROTO_MESSAGE + ".Builder")
.namedAnyOf("build", "buildPartial"));

private static final Matcher<ExpressionTree> OBJECT_METHODS =
anyOf(
instanceMethod()
.onDescendantOf("java.lang.Object")
.namedAnyOf("getClass", "hashCode", "clone", "toString")
.withParameters(),
instanceMethod()
.onDescendantOf("java.lang.Object")
.namedAnyOf("equals")
.withParameters("java.lang.Object"));

private static final Matcher<? super ExpressionTree> SPECIALIZED_MATCHER =
anyOf(
RETURNS_SAME_TYPE,
Expand All @@ -303,12 +314,14 @@ private static boolean functionalMethod(ExpressionTree tree, VisitorState state)
public ReturnValueIgnored(ErrorProneFlags flags) {
boolean checkOptional = flags.getBoolean("ReturnValueIgnored:MoreOptional").orElse(true);
boolean checkMoreMap = flags.getBoolean("ReturnValueIgnored:MoreMap").orElse(true);
boolean objectMethods = flags.getBoolean("ReturnValueIgnored:ObjectMethods").orElse(true);

this.matcher =
anyOf(
SPECIALIZED_MATCHER,
checkOptional ? MORE_OPTIONAL_METHODS : nothing(),
checkMoreMap ? MORE_MAP_METHODS : nothing());
checkMoreMap ? MORE_MAP_METHODS : nothing(),
objectMethods ? OBJECT_METHODS : nothing());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,4 +550,24 @@ public void testCollectionToArray_java8() {
"}")
.doTest();
}

@Test
public void objectMethods() {
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" void test(Test t, Object o) {",
" // BUG: Diagnostic contains:",
" t.equals(o);",
" // BUG: Diagnostic contains:",
" o.equals(t);",
" // BUG: Diagnostic contains:",
" t.hashCode();",
" // BUG: Diagnostic contains:",
" t.getClass();",
" }",
"}")
.doTest();
}
}

0 comments on commit e9aa66a

Please sign in to comment.