From 4f6b368cbad9d3edd4c405cfa91f914cee345966 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 26 Mar 2024 17:56:09 -0700 Subject: [PATCH] Fix an ArrayToString false positive for the varargs overload of `Joiner.join(Object, Object, Object...)` https://github.com/google/error-prone/pull/4233#pullrequestreview-1959627439 PiperOrigin-RevId: 619367171 --- .../bugpatterns/AbstractToString.java | 3 +- .../bugpatterns/ArrayToStringTest.java | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractToString.java b/core/src/main/java/com/google/errorprone/bugpatterns/AbstractToString.java index 94feba2dc590..7b20727d242c 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractToString.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/AbstractToString.java @@ -201,8 +201,9 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState argTree, ToStringKind.IMPLICIT, state); + } else { + handleStringifiedTree(argTree, ToStringKind.IMPLICIT, state); } - handleStringifiedTree(argTree, ToStringKind.IMPLICIT, state); } } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/ArrayToStringTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/ArrayToStringTest.java index 1014d8798c23..73b13cbdc110 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/ArrayToStringTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/ArrayToStringTest.java @@ -199,4 +199,33 @@ public void arrayPassedToJoiner() { "}") .doTest(); } + + @Test + public void arrayPassedToJoiner_firstSecondRest_negative() { + compilationHelper + .addSourceLines( + "Test.java", + "import com.google.common.base.Joiner;", + "class Test {", + " String test(Joiner j, Object first, Object second, Object[] rest) {", + " return j.join(first, second, rest);", + " }", + "}") + .doTest(); + } + + @Test + public void arrayPassedToJoiner_firstSecondRest_positive() { + compilationHelper + .addSourceLines( + "Test.java", + "import com.google.common.base.Joiner;", + "class Test {", + " String test(Joiner j, Object first, Object second, Object third, Object[] rest) {", + " // BUG: Diagnostic contains:", + " return j.join(first, second, third, rest);", + " }", + "}") + .doTest(); + } }