Skip to content

Commit 2076b8e

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Address more Error Prone warnings.
- mostly https://errorprone.info/bugpattern/UnnecessaryParentheses (by removing the parentheses) - in `HostAndPort`, https://errorprone.info/bugpattern/UnusedException (by using `tryParse` instead of a `try`-`catch` approach, conveniently also getting the behavior we want for `+` and for non-ASCII digits for free) - in `IntMath`, https://errorprone.info/bugpattern/NonFinalStaticField (by making it `final` :)) RELNOTES=n/a PiperOrigin-RevId: 729533506
1 parent 8f6f4b3 commit 2076b8e

File tree

9 files changed

+45
-58
lines changed

9 files changed

+45
-58
lines changed

android/guava/src/com/google/common/collect/CompactLinkedHashMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int getSuccessor(int entry) {
150150
}
151151

152152
private void setSuccessor(int entry, int succ) {
153-
long succMask = (~0L) >>> 32;
153+
long succMask = ~0L >>> 32;
154154
setLink(entry, (link(entry) & ~succMask) | ((succ + 1) & succMask));
155155
}
156156

android/guava/src/com/google/common/math/IntMath.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ public static int pow(int b, int k) {
225225
return (k == 0) ? 1 : 0;
226226
case 1:
227227
return 1;
228-
case (-1):
228+
case -1:
229229
return ((k & 1) == 0) ? 1 : -1;
230230
case 2:
231231
return (k < Integer.SIZE) ? (1 << k) : 0;
232-
case (-2):
232+
case -2:
233233
if (k < Integer.SIZE) {
234234
return ((k & 1) == 0) ? (1 << k) : -(1 << k);
235235
} else {
@@ -490,12 +490,12 @@ public static int checkedPow(int b, int k) {
490490
return (k == 0) ? 1 : 0;
491491
case 1:
492492
return 1;
493-
case (-1):
493+
case -1:
494494
return ((k & 1) == 0) ? 1 : -1;
495495
case 2:
496496
checkNoOverflow(k < Integer.SIZE - 1, "checkedPow", b, k);
497497
return 1 << k;
498-
case (-2):
498+
case -2:
499499
checkNoOverflow(k < Integer.SIZE, "checkedPow", b, k);
500500
return ((k & 1) == 0) ? 1 << k : -1 << k;
501501
default:
@@ -566,14 +566,14 @@ public static int saturatedPow(int b, int k) {
566566
return (k == 0) ? 1 : 0;
567567
case 1:
568568
return 1;
569-
case (-1):
569+
case -1:
570570
return ((k & 1) == 0) ? 1 : -1;
571571
case 2:
572572
if (k >= Integer.SIZE - 1) {
573573
return Integer.MAX_VALUE;
574574
}
575575
return 1 << k;
576-
case (-2):
576+
case -2:
577577
if (k >= Integer.SIZE) {
578578
return Integer.MAX_VALUE + (k & 1);
579579
}
@@ -667,7 +667,7 @@ public static int binomial(int n, int k) {
667667

668668
// binomial(biggestBinomials[k], k) fits in an int, but not binomial(biggestBinomials[k]+1,k).
669669
@VisibleForTesting
670-
static int[] biggestBinomials = {
670+
static final int[] biggestBinomials = {
671671
Integer.MAX_VALUE,
672672
Integer.MAX_VALUE,
673673
65536,

android/guava/src/com/google/common/math/LongMath.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ public static long pow(long b, int k) {
267267
return (k == 0) ? 1 : 0;
268268
case 1:
269269
return 1;
270-
case (-1):
270+
case -1:
271271
return ((k & 1) == 0) ? 1 : -1;
272272
case 2:
273273
return (k < Long.SIZE) ? 1L << k : 0;
274-
case (-2):
274+
case -2:
275275
if (k < Long.SIZE) {
276276
return ((k & 1) == 0) ? 1L << k : -(1L << k);
277277
} else {
@@ -605,12 +605,12 @@ public static long checkedPow(long b, int k) {
605605
return (k == 0) ? 1 : 0;
606606
case 1:
607607
return 1;
608-
case (-1):
608+
case -1:
609609
return ((k & 1) == 0) ? 1 : -1;
610610
case 2:
611611
checkNoOverflow(k < Long.SIZE - 1, "checkedPow", b, k);
612612
return 1L << k;
613-
case (-2):
613+
case -2:
614614
checkNoOverflow(k < Long.SIZE, "checkedPow", b, k);
615615
return ((k & 1) == 0) ? (1L << k) : (-1L << k);
616616
default:
@@ -723,14 +723,14 @@ public static long saturatedPow(long b, int k) {
723723
return (k == 0) ? 1 : 0;
724724
case 1:
725725
return 1;
726-
case (-1):
726+
case -1:
727727
return ((k & 1) == 0) ? 1 : -1;
728728
case 2:
729729
if (k >= Long.SIZE - 1) {
730730
return Long.MAX_VALUE;
731731
}
732732
return 1L << k;
733-
case (-2):
733+
case -2:
734734
if (k >= Long.SIZE) {
735735
return Long.MAX_VALUE + (k & 1);
736736
}

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919
import static com.google.common.base.Preconditions.checkState;
20+
import static com.google.common.base.Strings.isNullOrEmpty;
2021

2122
import com.google.common.annotations.GwtCompatible;
2223
import com.google.common.annotations.GwtIncompatible;
2324
import com.google.common.annotations.J2ktIncompatible;
24-
import com.google.common.base.CharMatcher;
2525
import com.google.common.base.Objects;
26-
import com.google.common.base.Strings;
26+
import com.google.common.primitives.Ints;
2727
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2828
import com.google.errorprone.annotations.Immutable;
2929
import java.io.Serializable;
@@ -188,19 +188,12 @@ public static HostAndPort fromString(String hostPortString) {
188188
}
189189
}
190190

191-
int port = NO_PORT;
192-
if (!Strings.isNullOrEmpty(portString)) {
193-
// Try to parse the whole port string as a number.
194-
// Java accepts leading plus signs. We don't want to.
195-
checkArgument(
196-
!portString.startsWith("+") && CharMatcher.ascii().matchesAllOf(portString),
197-
"Unparseable port number: %s",
198-
hostPortString);
199-
try {
200-
port = Integer.parseInt(portString);
201-
} catch (NumberFormatException e) {
202-
throw new IllegalArgumentException("Unparseable port number: " + hostPortString);
203-
}
191+
Integer port;
192+
if (isNullOrEmpty(portString)) {
193+
port = NO_PORT;
194+
} else {
195+
port = Ints.tryParse(portString);
196+
checkArgument(port != null, "Unparseable port number: %s", hostPortString);
204197
checkArgument(isValidPort(port), "Port number out of range: %s", hostPortString);
205198
}
206199

guava-gwt/src/com/google/common/net/Net.gwt.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<inherits name="com.google.common.base.Base" />
3535
<inherits name="com.google.common.collect.Collect" />
3636
<inherits name="com.google.common.escape.Escape" />
37+
<inherits name="com.google.common.primitives.Primitives" />
3738
<inherits name="com.google.gwt.core.Core" />
3839
<inherits name="com.google.gwt.user.User" />
3940
<inherits name="com.google.thirdparty.publicsuffix.PublicSuffixPatterns" />

guava/src/com/google/common/collect/CompactLinkedHashMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ int getSuccessor(int entry) {
155155
}
156156

157157
private void setSuccessor(int entry, int succ) {
158-
long succMask = (~0L) >>> 32;
158+
long succMask = ~0L >>> 32;
159159
setLink(entry, (link(entry) & ~succMask) | ((succ + 1) & succMask));
160160
}
161161

guava/src/com/google/common/math/IntMath.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ public static int pow(int b, int k) {
225225
return (k == 0) ? 1 : 0;
226226
case 1:
227227
return 1;
228-
case (-1):
228+
case -1:
229229
return ((k & 1) == 0) ? 1 : -1;
230230
case 2:
231231
return (k < Integer.SIZE) ? (1 << k) : 0;
232-
case (-2):
232+
case -2:
233233
if (k < Integer.SIZE) {
234234
return ((k & 1) == 0) ? (1 << k) : -(1 << k);
235235
} else {
@@ -490,12 +490,12 @@ public static int checkedPow(int b, int k) {
490490
return (k == 0) ? 1 : 0;
491491
case 1:
492492
return 1;
493-
case (-1):
493+
case -1:
494494
return ((k & 1) == 0) ? 1 : -1;
495495
case 2:
496496
checkNoOverflow(k < Integer.SIZE - 1, "checkedPow", b, k);
497497
return 1 << k;
498-
case (-2):
498+
case -2:
499499
checkNoOverflow(k < Integer.SIZE, "checkedPow", b, k);
500500
return ((k & 1) == 0) ? 1 << k : -1 << k;
501501
default:
@@ -566,14 +566,14 @@ public static int saturatedPow(int b, int k) {
566566
return (k == 0) ? 1 : 0;
567567
case 1:
568568
return 1;
569-
case (-1):
569+
case -1:
570570
return ((k & 1) == 0) ? 1 : -1;
571571
case 2:
572572
if (k >= Integer.SIZE - 1) {
573573
return Integer.MAX_VALUE;
574574
}
575575
return 1 << k;
576-
case (-2):
576+
case -2:
577577
if (k >= Integer.SIZE) {
578578
return Integer.MAX_VALUE + (k & 1);
579579
}
@@ -667,7 +667,7 @@ public static int binomial(int n, int k) {
667667

668668
// binomial(biggestBinomials[k], k) fits in an int, but not binomial(biggestBinomials[k]+1,k).
669669
@VisibleForTesting
670-
static int[] biggestBinomials = {
670+
static final int[] biggestBinomials = {
671671
Integer.MAX_VALUE,
672672
Integer.MAX_VALUE,
673673
65536,

guava/src/com/google/common/math/LongMath.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ public static long pow(long b, int k) {
267267
return (k == 0) ? 1 : 0;
268268
case 1:
269269
return 1;
270-
case (-1):
270+
case -1:
271271
return ((k & 1) == 0) ? 1 : -1;
272272
case 2:
273273
return (k < Long.SIZE) ? 1L << k : 0;
274-
case (-2):
274+
case -2:
275275
if (k < Long.SIZE) {
276276
return ((k & 1) == 0) ? 1L << k : -(1L << k);
277277
} else {
@@ -605,12 +605,12 @@ public static long checkedPow(long b, int k) {
605605
return (k == 0) ? 1 : 0;
606606
case 1:
607607
return 1;
608-
case (-1):
608+
case -1:
609609
return ((k & 1) == 0) ? 1 : -1;
610610
case 2:
611611
checkNoOverflow(k < Long.SIZE - 1, "checkedPow", b, k);
612612
return 1L << k;
613-
case (-2):
613+
case -2:
614614
checkNoOverflow(k < Long.SIZE, "checkedPow", b, k);
615615
return ((k & 1) == 0) ? (1L << k) : (-1L << k);
616616
default:
@@ -723,14 +723,14 @@ public static long saturatedPow(long b, int k) {
723723
return (k == 0) ? 1 : 0;
724724
case 1:
725725
return 1;
726-
case (-1):
726+
case -1:
727727
return ((k & 1) == 0) ? 1 : -1;
728728
case 2:
729729
if (k >= Long.SIZE - 1) {
730730
return Long.MAX_VALUE;
731731
}
732732
return 1L << k;
733-
case (-2):
733+
case -2:
734734
if (k >= Long.SIZE) {
735735
return Long.MAX_VALUE + (k & 1);
736736
}

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919
import static com.google.common.base.Preconditions.checkState;
20+
import static com.google.common.base.Strings.isNullOrEmpty;
2021

2122
import com.google.common.annotations.GwtCompatible;
2223
import com.google.common.annotations.GwtIncompatible;
2324
import com.google.common.annotations.J2ktIncompatible;
24-
import com.google.common.base.CharMatcher;
2525
import com.google.common.base.Objects;
26-
import com.google.common.base.Strings;
26+
import com.google.common.primitives.Ints;
2727
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2828
import com.google.errorprone.annotations.Immutable;
2929
import java.io.Serializable;
@@ -188,19 +188,12 @@ public static HostAndPort fromString(String hostPortString) {
188188
}
189189
}
190190

191-
int port = NO_PORT;
192-
if (!Strings.isNullOrEmpty(portString)) {
193-
// Try to parse the whole port string as a number.
194-
// Java accepts leading plus signs. We don't want to.
195-
checkArgument(
196-
!portString.startsWith("+") && CharMatcher.ascii().matchesAllOf(portString),
197-
"Unparseable port number: %s",
198-
hostPortString);
199-
try {
200-
port = Integer.parseInt(portString);
201-
} catch (NumberFormatException e) {
202-
throw new IllegalArgumentException("Unparseable port number: " + hostPortString);
203-
}
191+
Integer port;
192+
if (isNullOrEmpty(portString)) {
193+
port = NO_PORT;
194+
} else {
195+
port = Ints.tryParse(portString);
196+
checkArgument(port != null, "Unparseable port number: %s", hostPortString);
204197
checkArgument(isValidPort(port), "Port number out of range: %s", hostPortString);
205198
}
206199

0 commit comments

Comments
 (0)