From d0c23aabb89cdbde842a253b5f0f1eda4984da8d Mon Sep 17 00:00:00 2001 From: cpovirk Date: Wed, 12 Feb 2025 13:18:38 -0800 Subject: [PATCH] Suppress some https://errorprone.info/bugpattern/NullableOptional warnings. RELNOTES=n/a PiperOrigin-RevId: 726172349 --- android/guava/src/com/google/common/base/Optional.java | 6 +++++- android/guava/src/com/google/common/net/MediaType.java | 5 ++++- guava/src/com/google/common/base/Optional.java | 6 +++++- guava/src/com/google/common/net/MediaType.java | 5 ++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/android/guava/src/com/google/common/base/Optional.java b/android/guava/src/com/google/common/base/Optional.java index 8bcd68dd854e..041a91226956 100644 --- a/android/guava/src/com/google/common/base/Optional.java +++ b/android/guava/src/com/google/common/base/Optional.java @@ -127,7 +127,10 @@ public static Optional fromNullable(@Nullable T nullableReference) { * * @since 33.4.0 (but since 21.0 in the JRE flavor) */ - @SuppressWarnings("Java7ApiChecker") + @SuppressWarnings({ + "NullableOptional", // Null passthrough is reasonable for type conversions + "Java7ApiChecker", + }) @IgnoreJRERequirement // Users will use this only if they're already using Optional. public static @Nullable Optional fromJavaUtil( java.util.@Nullable Optional javaUtilOptional) { @@ -149,6 +152,7 @@ public static Optional fromNullable(@Nullable T nullableReference) { */ @SuppressWarnings({ "AmbiguousMethodReference", // We chose the name despite knowing this risk. + "NullableOptional", // Null passthrough is reasonable for type conversions "Java7ApiChecker", }) // If users use this when they shouldn't, we hope that NewApi will catch subsequent Optional calls diff --git a/android/guava/src/com/google/common/net/MediaType.java b/android/guava/src/com/google/common/net/MediaType.java index a1fa1b561d95..c6f0801e163d 100644 --- a/android/guava/src/com/google/common/net/MediaType.java +++ b/android/guava/src/com/google/common/net/MediaType.java @@ -811,7 +811,10 @@ private static MediaType addKnownType(MediaType mediaType) { @LazyInit private int hashCode; - @LazyInit private @Nullable Optional parsedCharset; + // We need to differentiate between "not computed" and "computed to be absent." + @SuppressWarnings("NullableOptional") + @LazyInit + private @Nullable Optional parsedCharset; private MediaType(String type, String subtype, ImmutableListMultimap parameters) { this.type = type; diff --git a/guava/src/com/google/common/base/Optional.java b/guava/src/com/google/common/base/Optional.java index bf652f491b79..85497aaee48a 100644 --- a/guava/src/com/google/common/base/Optional.java +++ b/guava/src/com/google/common/base/Optional.java @@ -127,6 +127,7 @@ public static Optional fromNullable(@Nullable T nullableReference) { * * @since 21.0 (but only since 33.4.0 in the Android flavor) */ + @SuppressWarnings("NullableOptional") // Null passthrough is reasonable for type conversions public static @Nullable Optional fromJavaUtil( java.util.@Nullable Optional javaUtilOptional) { return (javaUtilOptional == null) ? null : fromNullable(javaUtilOptional.orElse(null)); @@ -145,7 +146,10 @@ public static Optional fromNullable(@Nullable T nullableReference) { * * @since 21.0 (but only since 33.4.0 in the Android flavor) */ - @SuppressWarnings("AmbiguousMethodReference") // We chose the name despite knowing this risk. + @SuppressWarnings({ + "AmbiguousMethodReference", // We chose the name despite knowing this risk. + "NullableOptional", // Null passthrough is reasonable for type conversions + }) public static java.util.@Nullable Optional toJavaUtil( @Nullable Optional googleOptional) { return googleOptional == null ? null : googleOptional.toJavaUtil(); diff --git a/guava/src/com/google/common/net/MediaType.java b/guava/src/com/google/common/net/MediaType.java index a1fa1b561d95..c6f0801e163d 100644 --- a/guava/src/com/google/common/net/MediaType.java +++ b/guava/src/com/google/common/net/MediaType.java @@ -811,7 +811,10 @@ private static MediaType addKnownType(MediaType mediaType) { @LazyInit private int hashCode; - @LazyInit private @Nullable Optional parsedCharset; + // We need to differentiate between "not computed" and "computed to be absent." + @SuppressWarnings("NullableOptional") + @LazyInit + private @Nullable Optional parsedCharset; private MediaType(String type, String subtype, ImmutableListMultimap parameters) { this.type = type;