Skip to content

Commit

Permalink
Add @CheckReturnValue to com.google.common.net (with a few except…
Browse files Browse the repository at this point in the history
…ions).

RELNOTES=`net`: Added `@CheckReturnValue` to the package (with a few exceptions).
PiperOrigin-RevId: 429560962
  • Loading branch information
kluever authored and Google Java Core Libraries committed Feb 18, 2022
1 parent 6652237 commit a0e2577
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions android/guava/src/com/google/common/net/HostAndPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion android/guava/src/com/google/common/net/HostSpecifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions android/guava/src/com/google/common/net/InetAddresses.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -204,6 +205,7 @@ private int findSuffixOfType(Optional<PublicSuffixType> 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));
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions android/guava/src/com/google/common/net/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1085,6 +1087,7 @@ private static final class Tokenizer {
this.input = input;
}

@CanIgnoreReturnValue
String consumeTokenIfPresent(CharMatcher matcher) {
checkState(hasMore());
int startPosition = position;
Expand All @@ -1107,6 +1110,7 @@ char consumeCharacter(CharMatcher matcher) {
return c;
}

@CanIgnoreReturnValue
char consumeCharacter(char c) {
checkState(hasMore());
checkState(previewChar() == c);
Expand Down
2 changes: 2 additions & 0 deletions android/guava/src/com/google/common/net/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
*
* @author Craig Berry
*/
@CheckReturnValue
@ParametersAreNonnullByDefault
package com.google.common.net;

import com.google.errorprone.annotations.CheckReturnValue;
import javax.annotation.ParametersAreNonnullByDefault;
5 changes: 3 additions & 2 deletions guava-tests/test/com/google/common/net/HostSpecifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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() {
Expand Down
13 changes: 4 additions & 9 deletions guava-tests/test/com/google/common/net/PercentEscaperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions guava/src/com/google/common/net/HostAndPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion guava/src/com/google/common/net/HostSpecifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions guava/src/com/google/common/net/InetAddresses.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion guava/src/com/google/common/net/InternetDomainName.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -204,6 +205,7 @@ private int findSuffixOfType(Optional<PublicSuffixType> 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));
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions guava/src/com/google/common/net/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1085,6 +1087,7 @@ private static final class Tokenizer {
this.input = input;
}

@CanIgnoreReturnValue
String consumeTokenIfPresent(CharMatcher matcher) {
checkState(hasMore());
int startPosition = position;
Expand All @@ -1107,6 +1110,7 @@ char consumeCharacter(CharMatcher matcher) {
return c;
}

@CanIgnoreReturnValue
char consumeCharacter(char c) {
checkState(hasMore());
checkState(previewChar() == c);
Expand Down
2 changes: 2 additions & 0 deletions guava/src/com/google/common/net/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
*
* @author Craig Berry
*/
@CheckReturnValue
@ParametersAreNonnullByDefault
package com.google.common.net;

import com.google.errorprone.annotations.CheckReturnValue;
import javax.annotation.ParametersAreNonnullByDefault;

0 comments on commit a0e2577

Please sign in to comment.