From a0e2577de61a0d7e8a3dd075be66a31c93ea0446 Mon Sep 17 00:00:00 2001 From: Kurt Alfred Kluever Date: Fri, 18 Feb 2022 07:28:38 -0800 Subject: [PATCH] Add `@CheckReturnValue` to `com.google.common.net` (with a few exceptions). RELNOTES=`net`: Added `@CheckReturnValue` to the package (with a few exceptions). PiperOrigin-RevId: 429560962 --- .../com/google/common/net/HostSpecifierTest.java | 5 +++-- .../google/common/net/InternetDomainNameTest.java | 4 ++-- .../com/google/common/net/PercentEscaperTest.java | 13 ++++--------- .../src/com/google/common/net/HostAndPort.java | 3 +++ .../src/com/google/common/net/HostSpecifier.java | 4 +++- .../src/com/google/common/net/InetAddresses.java | 2 ++ .../com/google/common/net/InternetDomainName.java | 4 +++- .../guava/src/com/google/common/net/MediaType.java | 4 ++++ .../src/com/google/common/net/package-info.java | 2 ++ .../com/google/common/net/HostSpecifierTest.java | 5 +++-- .../google/common/net/InternetDomainNameTest.java | 4 ++-- .../com/google/common/net/PercentEscaperTest.java | 13 ++++--------- guava/src/com/google/common/net/HostAndPort.java | 3 +++ guava/src/com/google/common/net/HostSpecifier.java | 4 +++- guava/src/com/google/common/net/InetAddresses.java | 2 ++ .../com/google/common/net/InternetDomainName.java | 4 +++- guava/src/com/google/common/net/MediaType.java | 4 ++++ guava/src/com/google/common/net/package-info.java | 2 ++ 18 files changed, 52 insertions(+), 30 deletions(-) diff --git a/android/guava-tests/test/com/google/common/net/HostSpecifierTest.java b/android/guava-tests/test/com/google/common/net/HostSpecifierTest.java index fadeff7aadf4..8112879a86d9 100644 --- a/android/guava-tests/test/com/google/common/net/HostSpecifierTest.java +++ b/android/guava-tests/test/com/google/common/net/HostSpecifierTest.java @@ -92,8 +92,9 @@ public void testNulls() { } private void assertGood(String spec) throws ParseException { - HostSpecifier.fromValid(spec); // Throws exception if not working correctly - HostSpecifier.from(spec); + // Throws exception if not working correctly + HostSpecifier unused = HostSpecifier.fromValid(spec); + unused = HostSpecifier.from(spec); assertTrue(HostSpecifier.isValid(spec)); } diff --git a/android/guava-tests/test/com/google/common/net/InternetDomainNameTest.java b/android/guava-tests/test/com/google/common/net/InternetDomainNameTest.java index 7113fb45741a..c440ee53f509 100644 --- a/android/guava-tests/test/com/google/common/net/InternetDomainNameTest.java +++ b/android/guava-tests/test/com/google/common/net/InternetDomainNameTest.java @@ -232,7 +232,7 @@ public final class InternetDomainNameTest extends TestCase { public void testValid() { for (String name : VALID_NAME) { - InternetDomainName.from(name); + InternetDomainName unused = InternetDomainName.from(name); } } @@ -392,7 +392,7 @@ public void testParentChild() { // These would throw an exception if leniency were not preserved during parent() and child() // calls. InternetDomainName child = parent.child(LOTS_OF_DELTAS); - child.child(LOTS_OF_DELTAS); + InternetDomainName unused = child.child(LOTS_OF_DELTAS); } public void testValidTopPrivateDomain() { diff --git a/android/guava-tests/test/com/google/common/net/PercentEscaperTest.java b/android/guava-tests/test/com/google/common/net/PercentEscaperTest.java index 8443680e7f11..e600e4c7c1af 100644 --- a/android/guava-tests/test/com/google/common/net/PercentEscaperTest.java +++ b/android/guava-tests/test/com/google/common/net/PercentEscaperTest.java @@ -120,16 +120,11 @@ public void testBadArguments_badchars() { } } - /** - * Tests that if space is a safe character you cannot also specify 'plusForSpace' (throws {@link - * IllegalArgumentException}). - */ public void testBadArguments_plusforspace() { - try { - new PercentEscaper(" ", false); - } catch (IllegalArgumentException e) { - fail("Space can be a 'safe' character if plusForSpace is false"); - } + // space can be a safe char if plusForSpace is false + PercentEscaper unused = new PercentEscaper(" ", false); + + // space cannot be a safe char is plusForSpace is true String msg = "plusForSpace cannot be specified when space is a 'safe' character"; try { new PercentEscaper(" ", true); diff --git a/android/guava/src/com/google/common/net/HostAndPort.java b/android/guava/src/com/google/common/net/HostAndPort.java index 10eea27f81a9..a27eb65ff3ba 100644 --- a/android/guava/src/com/google/common/net/HostAndPort.java +++ b/android/guava/src/com/google/common/net/HostAndPort.java @@ -22,6 +22,7 @@ import com.google.common.base.CharMatcher; import com.google.common.base.Objects; import com.google.common.base.Strings; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.Immutable; import java.io.Serializable; import javax.annotation.CheckForNull; @@ -162,6 +163,7 @@ public static HostAndPort fromHost(String host) { * @return if parsing was successful, a populated HostAndPort object. * @throws IllegalArgumentException if nothing meaningful could be parsed. */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static HostAndPort fromString(String hostPortString) { checkNotNull(hostPortString); String host; @@ -272,6 +274,7 @@ public HostAndPort withDefaultPort(int defaultPort) { * @return {@code this}, to enable chaining of calls. * @throws IllegalArgumentException if bracketless IPv6 is detected. */ + @CanIgnoreReturnValue public HostAndPort requireBracketsForIPv6() { checkArgument(!hasBracketlessColons, "Possible bracketless IPv6 literal: %s", host); return this; diff --git a/android/guava/src/com/google/common/net/HostSpecifier.java b/android/guava/src/com/google/common/net/HostSpecifier.java index 55e74a7a1975..c57f3d97593c 100644 --- a/android/guava/src/com/google/common/net/HostSpecifier.java +++ b/android/guava/src/com/google/common/net/HostSpecifier.java @@ -16,6 +16,7 @@ import com.google.common.annotations.GwtIncompatible; import com.google.common.base.Preconditions; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.net.InetAddress; import java.text.ParseException; import javax.annotation.CheckForNull; @@ -108,6 +109,7 @@ public static HostSpecifier fromValid(String specifier) { * * @throws ParseException if the specifier is not valid. */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static HostSpecifier from(String specifier) throws ParseException { try { return fromValid(specifier); @@ -128,7 +130,7 @@ public static HostSpecifier from(String specifier) throws ParseException { */ public static boolean isValid(String specifier) { try { - fromValid(specifier); + HostSpecifier unused = fromValid(specifier); return true; } catch (IllegalArgumentException e) { return false; diff --git a/android/guava/src/com/google/common/net/InetAddresses.java b/android/guava/src/com/google/common/net/InetAddresses.java index aa9cda44ff02..d3e68db8ac6f 100644 --- a/android/guava/src/com/google/common/net/InetAddresses.java +++ b/android/guava/src/com/google/common/net/InetAddresses.java @@ -23,6 +23,7 @@ import com.google.common.hash.Hashing; import com.google.common.io.ByteStreams; import com.google.common.primitives.Ints; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.math.BigInteger; import java.net.Inet4Address; import java.net.Inet6Address; @@ -142,6 +143,7 @@ private static Inet4Address getInet4Address(byte[] bytes) { * @return {@link InetAddress} representing the argument * @throws IllegalArgumentException if the argument is not a valid IP string literal */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static InetAddress forString(String ipString) { byte[] addr = ipStringToBytes(ipString); diff --git a/android/guava/src/com/google/common/net/InternetDomainName.java b/android/guava/src/com/google/common/net/InternetDomainName.java index 902683cebf58..d71a9d5c3c6c 100644 --- a/android/guava/src/com/google/common/net/InternetDomainName.java +++ b/android/guava/src/com/google/common/net/InternetDomainName.java @@ -25,6 +25,7 @@ import com.google.common.base.Optional; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.Immutable; import com.google.thirdparty.publicsuffix.PublicSuffixPatterns; import com.google.thirdparty.publicsuffix.PublicSuffixType; @@ -204,6 +205,7 @@ private int findSuffixOfType(Optional desiredType) { * {@link #isValid} * @since 10.0 (previously named {@code fromLenient}) */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static InternetDomainName from(String domain) { return new InternetDomainName(checkNotNull(domain)); } @@ -582,7 +584,7 @@ public InternetDomainName child(String leftParts) { */ public static boolean isValid(String name) { try { - from(name); + InternetDomainName unused = from(name); return true; } catch (IllegalArgumentException e) { return false; diff --git a/android/guava/src/com/google/common/net/MediaType.java b/android/guava/src/com/google/common/net/MediaType.java index 65540228bb40..a713d0b17ae5 100644 --- a/android/guava/src/com/google/common/net/MediaType.java +++ b/android/guava/src/com/google/common/net/MediaType.java @@ -35,6 +35,7 @@ import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.Immutable; import com.google.errorprone.annotations.concurrent.LazyInit; import java.nio.charset.Charset; @@ -1038,6 +1039,7 @@ private static String normalizeParameterValue(String attribute, String value) { * * @throws IllegalArgumentException if the input is not parsable */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static MediaType parse(String input) { checkNotNull(input); Tokenizer tokenizer = new Tokenizer(input); @@ -1085,6 +1087,7 @@ private static final class Tokenizer { this.input = input; } + @CanIgnoreReturnValue String consumeTokenIfPresent(CharMatcher matcher) { checkState(hasMore()); int startPosition = position; @@ -1107,6 +1110,7 @@ char consumeCharacter(CharMatcher matcher) { return c; } + @CanIgnoreReturnValue char consumeCharacter(char c) { checkState(hasMore()); checkState(previewChar() == c); diff --git a/android/guava/src/com/google/common/net/package-info.java b/android/guava/src/com/google/common/net/package-info.java index d9db26637974..d55642d9431b 100644 --- a/android/guava/src/com/google/common/net/package-info.java +++ b/android/guava/src/com/google/common/net/package-info.java @@ -21,7 +21,9 @@ * * @author Craig Berry */ +@CheckReturnValue @ParametersAreNonnullByDefault package com.google.common.net; +import com.google.errorprone.annotations.CheckReturnValue; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/guava-tests/test/com/google/common/net/HostSpecifierTest.java b/guava-tests/test/com/google/common/net/HostSpecifierTest.java index fadeff7aadf4..8112879a86d9 100644 --- a/guava-tests/test/com/google/common/net/HostSpecifierTest.java +++ b/guava-tests/test/com/google/common/net/HostSpecifierTest.java @@ -92,8 +92,9 @@ public void testNulls() { } private void assertGood(String spec) throws ParseException { - HostSpecifier.fromValid(spec); // Throws exception if not working correctly - HostSpecifier.from(spec); + // Throws exception if not working correctly + HostSpecifier unused = HostSpecifier.fromValid(spec); + unused = HostSpecifier.from(spec); assertTrue(HostSpecifier.isValid(spec)); } diff --git a/guava-tests/test/com/google/common/net/InternetDomainNameTest.java b/guava-tests/test/com/google/common/net/InternetDomainNameTest.java index 7113fb45741a..c440ee53f509 100644 --- a/guava-tests/test/com/google/common/net/InternetDomainNameTest.java +++ b/guava-tests/test/com/google/common/net/InternetDomainNameTest.java @@ -232,7 +232,7 @@ public final class InternetDomainNameTest extends TestCase { public void testValid() { for (String name : VALID_NAME) { - InternetDomainName.from(name); + InternetDomainName unused = InternetDomainName.from(name); } } @@ -392,7 +392,7 @@ public void testParentChild() { // These would throw an exception if leniency were not preserved during parent() and child() // calls. InternetDomainName child = parent.child(LOTS_OF_DELTAS); - child.child(LOTS_OF_DELTAS); + InternetDomainName unused = child.child(LOTS_OF_DELTAS); } public void testValidTopPrivateDomain() { diff --git a/guava-tests/test/com/google/common/net/PercentEscaperTest.java b/guava-tests/test/com/google/common/net/PercentEscaperTest.java index 8443680e7f11..e600e4c7c1af 100644 --- a/guava-tests/test/com/google/common/net/PercentEscaperTest.java +++ b/guava-tests/test/com/google/common/net/PercentEscaperTest.java @@ -120,16 +120,11 @@ public void testBadArguments_badchars() { } } - /** - * Tests that if space is a safe character you cannot also specify 'plusForSpace' (throws {@link - * IllegalArgumentException}). - */ public void testBadArguments_plusforspace() { - try { - new PercentEscaper(" ", false); - } catch (IllegalArgumentException e) { - fail("Space can be a 'safe' character if plusForSpace is false"); - } + // space can be a safe char if plusForSpace is false + PercentEscaper unused = new PercentEscaper(" ", false); + + // space cannot be a safe char is plusForSpace is true String msg = "plusForSpace cannot be specified when space is a 'safe' character"; try { new PercentEscaper(" ", true); diff --git a/guava/src/com/google/common/net/HostAndPort.java b/guava/src/com/google/common/net/HostAndPort.java index 10eea27f81a9..a27eb65ff3ba 100644 --- a/guava/src/com/google/common/net/HostAndPort.java +++ b/guava/src/com/google/common/net/HostAndPort.java @@ -22,6 +22,7 @@ import com.google.common.base.CharMatcher; import com.google.common.base.Objects; import com.google.common.base.Strings; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.Immutable; import java.io.Serializable; import javax.annotation.CheckForNull; @@ -162,6 +163,7 @@ public static HostAndPort fromHost(String host) { * @return if parsing was successful, a populated HostAndPort object. * @throws IllegalArgumentException if nothing meaningful could be parsed. */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static HostAndPort fromString(String hostPortString) { checkNotNull(hostPortString); String host; @@ -272,6 +274,7 @@ public HostAndPort withDefaultPort(int defaultPort) { * @return {@code this}, to enable chaining of calls. * @throws IllegalArgumentException if bracketless IPv6 is detected. */ + @CanIgnoreReturnValue public HostAndPort requireBracketsForIPv6() { checkArgument(!hasBracketlessColons, "Possible bracketless IPv6 literal: %s", host); return this; diff --git a/guava/src/com/google/common/net/HostSpecifier.java b/guava/src/com/google/common/net/HostSpecifier.java index 55e74a7a1975..c57f3d97593c 100644 --- a/guava/src/com/google/common/net/HostSpecifier.java +++ b/guava/src/com/google/common/net/HostSpecifier.java @@ -16,6 +16,7 @@ import com.google.common.annotations.GwtIncompatible; import com.google.common.base.Preconditions; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.net.InetAddress; import java.text.ParseException; import javax.annotation.CheckForNull; @@ -108,6 +109,7 @@ public static HostSpecifier fromValid(String specifier) { * * @throws ParseException if the specifier is not valid. */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static HostSpecifier from(String specifier) throws ParseException { try { return fromValid(specifier); @@ -128,7 +130,7 @@ public static HostSpecifier from(String specifier) throws ParseException { */ public static boolean isValid(String specifier) { try { - fromValid(specifier); + HostSpecifier unused = fromValid(specifier); return true; } catch (IllegalArgumentException e) { return false; diff --git a/guava/src/com/google/common/net/InetAddresses.java b/guava/src/com/google/common/net/InetAddresses.java index aa9cda44ff02..d3e68db8ac6f 100644 --- a/guava/src/com/google/common/net/InetAddresses.java +++ b/guava/src/com/google/common/net/InetAddresses.java @@ -23,6 +23,7 @@ import com.google.common.hash.Hashing; import com.google.common.io.ByteStreams; import com.google.common.primitives.Ints; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.math.BigInteger; import java.net.Inet4Address; import java.net.Inet6Address; @@ -142,6 +143,7 @@ private static Inet4Address getInet4Address(byte[] bytes) { * @return {@link InetAddress} representing the argument * @throws IllegalArgumentException if the argument is not a valid IP string literal */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static InetAddress forString(String ipString) { byte[] addr = ipStringToBytes(ipString); diff --git a/guava/src/com/google/common/net/InternetDomainName.java b/guava/src/com/google/common/net/InternetDomainName.java index 902683cebf58..d71a9d5c3c6c 100644 --- a/guava/src/com/google/common/net/InternetDomainName.java +++ b/guava/src/com/google/common/net/InternetDomainName.java @@ -25,6 +25,7 @@ import com.google.common.base.Optional; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.Immutable; import com.google.thirdparty.publicsuffix.PublicSuffixPatterns; import com.google.thirdparty.publicsuffix.PublicSuffixType; @@ -204,6 +205,7 @@ private int findSuffixOfType(Optional desiredType) { * {@link #isValid} * @since 10.0 (previously named {@code fromLenient}) */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static InternetDomainName from(String domain) { return new InternetDomainName(checkNotNull(domain)); } @@ -582,7 +584,7 @@ public InternetDomainName child(String leftParts) { */ public static boolean isValid(String name) { try { - from(name); + InternetDomainName unused = from(name); return true; } catch (IllegalArgumentException e) { return false; diff --git a/guava/src/com/google/common/net/MediaType.java b/guava/src/com/google/common/net/MediaType.java index 65540228bb40..a713d0b17ae5 100644 --- a/guava/src/com/google/common/net/MediaType.java +++ b/guava/src/com/google/common/net/MediaType.java @@ -35,6 +35,7 @@ import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; +import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.Immutable; import com.google.errorprone.annotations.concurrent.LazyInit; import java.nio.charset.Charset; @@ -1038,6 +1039,7 @@ private static String normalizeParameterValue(String attribute, String value) { * * @throws IllegalArgumentException if the input is not parsable */ + @CanIgnoreReturnValue // TODO(b/219820829): consider removing public static MediaType parse(String input) { checkNotNull(input); Tokenizer tokenizer = new Tokenizer(input); @@ -1085,6 +1087,7 @@ private static final class Tokenizer { this.input = input; } + @CanIgnoreReturnValue String consumeTokenIfPresent(CharMatcher matcher) { checkState(hasMore()); int startPosition = position; @@ -1107,6 +1110,7 @@ char consumeCharacter(CharMatcher matcher) { return c; } + @CanIgnoreReturnValue char consumeCharacter(char c) { checkState(hasMore()); checkState(previewChar() == c); diff --git a/guava/src/com/google/common/net/package-info.java b/guava/src/com/google/common/net/package-info.java index d9db26637974..d55642d9431b 100644 --- a/guava/src/com/google/common/net/package-info.java +++ b/guava/src/com/google/common/net/package-info.java @@ -21,7 +21,9 @@ * * @author Craig Berry */ +@CheckReturnValue @ParametersAreNonnullByDefault package com.google.common.net; +import com.google.errorprone.annotations.CheckReturnValue; import javax.annotation.ParametersAreNonnullByDefault;