Skip to content

Commit a0e2577

Browse files
klueverGoogle Java Core Libraries
authored andcommitted
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
1 parent 6652237 commit a0e2577

File tree

18 files changed

+52
-30
lines changed

18 files changed

+52
-30
lines changed

android/guava-tests/test/com/google/common/net/HostSpecifierTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ public void testNulls() {
9292
}
9393

9494
private void assertGood(String spec) throws ParseException {
95-
HostSpecifier.fromValid(spec); // Throws exception if not working correctly
96-
HostSpecifier.from(spec);
95+
// Throws exception if not working correctly
96+
HostSpecifier unused = HostSpecifier.fromValid(spec);
97+
unused = HostSpecifier.from(spec);
9798
assertTrue(HostSpecifier.isValid(spec));
9899
}
99100

android/guava-tests/test/com/google/common/net/InternetDomainNameTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public final class InternetDomainNameTest extends TestCase {
232232

233233
public void testValid() {
234234
for (String name : VALID_NAME) {
235-
InternetDomainName.from(name);
235+
InternetDomainName unused = InternetDomainName.from(name);
236236
}
237237
}
238238

@@ -392,7 +392,7 @@ public void testParentChild() {
392392
// These would throw an exception if leniency were not preserved during parent() and child()
393393
// calls.
394394
InternetDomainName child = parent.child(LOTS_OF_DELTAS);
395-
child.child(LOTS_OF_DELTAS);
395+
InternetDomainName unused = child.child(LOTS_OF_DELTAS);
396396
}
397397

398398
public void testValidTopPrivateDomain() {

android/guava-tests/test/com/google/common/net/PercentEscaperTest.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,11 @@ public void testBadArguments_badchars() {
120120
}
121121
}
122122

123-
/**
124-
* Tests that if space is a safe character you cannot also specify 'plusForSpace' (throws {@link
125-
* IllegalArgumentException}).
126-
*/
127123
public void testBadArguments_plusforspace() {
128-
try {
129-
new PercentEscaper(" ", false);
130-
} catch (IllegalArgumentException e) {
131-
fail("Space can be a 'safe' character if plusForSpace is false");
132-
}
124+
// space can be a safe char if plusForSpace is false
125+
PercentEscaper unused = new PercentEscaper(" ", false);
126+
127+
// space cannot be a safe char is plusForSpace is true
133128
String msg = "plusForSpace cannot be specified when space is a 'safe' character";
134129
try {
135130
new PercentEscaper(" ", true);

android/guava/src/com/google/common/net/HostAndPort.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.base.CharMatcher;
2323
import com.google.common.base.Objects;
2424
import com.google.common.base.Strings;
25+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2526
import com.google.errorprone.annotations.Immutable;
2627
import java.io.Serializable;
2728
import javax.annotation.CheckForNull;
@@ -162,6 +163,7 @@ public static HostAndPort fromHost(String host) {
162163
* @return if parsing was successful, a populated HostAndPort object.
163164
* @throws IllegalArgumentException if nothing meaningful could be parsed.
164165
*/
166+
@CanIgnoreReturnValue // TODO(b/219820829): consider removing
165167
public static HostAndPort fromString(String hostPortString) {
166168
checkNotNull(hostPortString);
167169
String host;
@@ -272,6 +274,7 @@ public HostAndPort withDefaultPort(int defaultPort) {
272274
* @return {@code this}, to enable chaining of calls.
273275
* @throws IllegalArgumentException if bracketless IPv6 is detected.
274276
*/
277+
@CanIgnoreReturnValue
275278
public HostAndPort requireBracketsForIPv6() {
276279
checkArgument(!hasBracketlessColons, "Possible bracketless IPv6 literal: %s", host);
277280
return this;

android/guava/src/com/google/common/net/HostSpecifier.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.google.common.annotations.GwtIncompatible;
1818
import com.google.common.base.Preconditions;
19+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1920
import java.net.InetAddress;
2021
import java.text.ParseException;
2122
import javax.annotation.CheckForNull;
@@ -108,6 +109,7 @@ public static HostSpecifier fromValid(String specifier) {
108109
*
109110
* @throws ParseException if the specifier is not valid.
110111
*/
112+
@CanIgnoreReturnValue // TODO(b/219820829): consider removing
111113
public static HostSpecifier from(String specifier) throws ParseException {
112114
try {
113115
return fromValid(specifier);
@@ -128,7 +130,7 @@ public static HostSpecifier from(String specifier) throws ParseException {
128130
*/
129131
public static boolean isValid(String specifier) {
130132
try {
131-
fromValid(specifier);
133+
HostSpecifier unused = fromValid(specifier);
132134
return true;
133135
} catch (IllegalArgumentException e) {
134136
return false;

android/guava/src/com/google/common/net/InetAddresses.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.hash.Hashing;
2424
import com.google.common.io.ByteStreams;
2525
import com.google.common.primitives.Ints;
26+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2627
import java.math.BigInteger;
2728
import java.net.Inet4Address;
2829
import java.net.Inet6Address;
@@ -142,6 +143,7 @@ private static Inet4Address getInet4Address(byte[] bytes) {
142143
* @return {@link InetAddress} representing the argument
143144
* @throws IllegalArgumentException if the argument is not a valid IP string literal
144145
*/
146+
@CanIgnoreReturnValue // TODO(b/219820829): consider removing
145147
public static InetAddress forString(String ipString) {
146148
byte[] addr = ipStringToBytes(ipString);
147149

android/guava/src/com/google/common/net/InternetDomainName.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.base.Optional;
2626
import com.google.common.base.Splitter;
2727
import com.google.common.collect.ImmutableList;
28+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2829
import com.google.errorprone.annotations.Immutable;
2930
import com.google.thirdparty.publicsuffix.PublicSuffixPatterns;
3031
import com.google.thirdparty.publicsuffix.PublicSuffixType;
@@ -204,6 +205,7 @@ private int findSuffixOfType(Optional<PublicSuffixType> desiredType) {
204205
* {@link #isValid}
205206
* @since 10.0 (previously named {@code fromLenient})
206207
*/
208+
@CanIgnoreReturnValue // TODO(b/219820829): consider removing
207209
public static InternetDomainName from(String domain) {
208210
return new InternetDomainName(checkNotNull(domain));
209211
}
@@ -582,7 +584,7 @@ public InternetDomainName child(String leftParts) {
582584
*/
583585
public static boolean isValid(String name) {
584586
try {
585-
from(name);
587+
InternetDomainName unused = from(name);
586588
return true;
587589
} catch (IllegalArgumentException e) {
588590
return false;

android/guava/src/com/google/common/net/MediaType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.common.collect.Maps;
3636
import com.google.common.collect.Multimap;
3737
import com.google.common.collect.Multimaps;
38+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
3839
import com.google.errorprone.annotations.Immutable;
3940
import com.google.errorprone.annotations.concurrent.LazyInit;
4041
import java.nio.charset.Charset;
@@ -1038,6 +1039,7 @@ private static String normalizeParameterValue(String attribute, String value) {
10381039
*
10391040
* @throws IllegalArgumentException if the input is not parsable
10401041
*/
1042+
@CanIgnoreReturnValue // TODO(b/219820829): consider removing
10411043
public static MediaType parse(String input) {
10421044
checkNotNull(input);
10431045
Tokenizer tokenizer = new Tokenizer(input);
@@ -1085,6 +1087,7 @@ private static final class Tokenizer {
10851087
this.input = input;
10861088
}
10871089

1090+
@CanIgnoreReturnValue
10881091
String consumeTokenIfPresent(CharMatcher matcher) {
10891092
checkState(hasMore());
10901093
int startPosition = position;
@@ -1107,6 +1110,7 @@ char consumeCharacter(CharMatcher matcher) {
11071110
return c;
11081111
}
11091112

1113+
@CanIgnoreReturnValue
11101114
char consumeCharacter(char c) {
11111115
checkState(hasMore());
11121116
checkState(previewChar() == c);

android/guava/src/com/google/common/net/package-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
*
2222
* @author Craig Berry
2323
*/
24+
@CheckReturnValue
2425
@ParametersAreNonnullByDefault
2526
package com.google.common.net;
2627

28+
import com.google.errorprone.annotations.CheckReturnValue;
2729
import javax.annotation.ParametersAreNonnullByDefault;

guava-tests/test/com/google/common/net/HostSpecifierTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ public void testNulls() {
9292
}
9393

9494
private void assertGood(String spec) throws ParseException {
95-
HostSpecifier.fromValid(spec); // Throws exception if not working correctly
96-
HostSpecifier.from(spec);
95+
// Throws exception if not working correctly
96+
HostSpecifier unused = HostSpecifier.fromValid(spec);
97+
unused = HostSpecifier.from(spec);
9798
assertTrue(HostSpecifier.isValid(spec));
9899
}
99100

0 commit comments

Comments
 (0)