From 611972f672bb1258d15571bec28f1a53692f56b7 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Mon, 13 Oct 2025 13:21:03 -0700 Subject: [PATCH] Suppress or address errors from a forthcoming Error Prone check that looks for calls like `containsExactly(null)` that always throw NPE. (To check for a list that contains only a null element, the calls will soon need to be `containsExactly((Object) null)` (with a cast to the array's _element_ type), not `containsExactly(null)` or `containsExactly((Object[]) null)` (with a cast to the _array_ type).) Also, sidestep other null-varargs fun by using `Collections.singleton` instead of the varargs methods `Arrays.asList` and `Stream.of`.) ([the check](https://github.com/google/error-prone/pull/5263)) PiperOrigin-RevId: 818803000 --- .../java/com/google/common/truth/IterableSubjectTest.java | 7 +++++-- .../java/com/google/common/truth/StreamSubjectTest.java | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/src/test/java/com/google/common/truth/IterableSubjectTest.java b/core/src/test/java/com/google/common/truth/IterableSubjectTest.java index 71254721c..8f012a2f1 100644 --- a/core/src/test/java/com/google/common/truth/IterableSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/IterableSubjectTest.java @@ -23,6 +23,7 @@ import static com.google.common.truth.Truth.assertThat; import static java.lang.String.CASE_INSENSITIVE_ORDER; import static java.util.Arrays.asList; +import static java.util.Collections.singleton; import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; @@ -814,10 +815,12 @@ public void containsExactlyWithDuplicatesOutOfOrder() { } @Test + // We intentionally test the behavior of the method under a call that will soon fail. + @SuppressWarnings("NullNeedsCastForVarargs") @J2ktIncompatible // Kotlin can't pass a null array for a varargs parameter public void containsExactlyWithOnlyNullPassedAsNullArray() { - // Truth is tolerant of this erroneous varargs call. - Iterable actual = asList((Object) null); + Iterable actual = singleton(null); + // We are changing Truth from tolerating this erroneous varargs call to not tolerating it. try { assertThat(actual).containsExactly((Object[]) null); } catch (NullPointerException e) { diff --git a/core/src/test/java/com/google/common/truth/StreamSubjectTest.java b/core/src/test/java/com/google/common/truth/StreamSubjectTest.java index 9ce30bcce..d2b1c260d 100644 --- a/core/src/test/java/com/google/common/truth/StreamSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/StreamSubjectTest.java @@ -21,6 +21,7 @@ import static com.google.common.truth.FailureAssertions.assertFailureValue; import static com.google.common.truth.Truth.assertThat; import static java.util.Arrays.asList; +import static java.util.Collections.singleton; import java.util.stream.Stream; import org.junit.Test; @@ -316,8 +317,11 @@ public void containsExactly_nullObject() { @Test @J2ktIncompatible // Kotlin can't pass a null array for a varargs parameter + // We intentionally test the behavior of the method under a call that will soon fail. + @SuppressWarnings("NullNeedsCastForVarargs") public void containsExactly_nullObjectArray() { - StreamSubject subject = assertThat(Stream.of((Object) null)); + StreamSubject subject = assertThat(singleton(null).stream()); + // We are changing Truth from tolerating this erroneous varargs call to not tolerating it. try { subject.containsExactly((Object[]) null); } catch (NullPointerException e) {