Skip to content

Commit ad5371e

Browse files
Fix findings that are new with the latest Error Prone. (#2834)
1 parent de190d7 commit ad5371e

File tree

7 files changed

+56
-25
lines changed

7 files changed

+56
-25
lines changed

gson/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<dependency>
5151
<groupId>com.google.errorprone</groupId>
5252
<artifactId>error_prone_annotations</artifactId>
53-
<version>2.36.0</version>
53+
<version>2.37.0</version>
5454
</dependency>
5555

5656
<dependency>

gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,19 @@ private void rotateRight(Node<K, V> root) {
451451
@Override
452452
public Set<Entry<K, V>> entrySet() {
453453
EntrySet result = entrySet;
454-
return result != null ? result : (entrySet = new EntrySet());
454+
if (result == null) {
455+
result = entrySet = new EntrySet();
456+
}
457+
return result;
455458
}
456459

457460
@Override
458461
public Set<K> keySet() {
459462
KeySet result = keySet;
460-
return result != null ? result : (keySet = new KeySet());
463+
if (result == null) {
464+
result = keySet = new KeySet();
465+
}
466+
return result;
461467
}
462468

463469
static final class Node<K, V> implements Entry<K, V> {
@@ -576,7 +582,8 @@ final Node<K, V> nextNode() {
576582
throw new ConcurrentModificationException();
577583
}
578584
next = e.next;
579-
return lastReturned = e;
585+
lastReturned = e;
586+
return e;
580587
}
581588

582589
@Override

gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ private TypeAdapter<T> delegate() {
113113
// A race might lead to `delegate` being assigned by multiple threads but the last assignment
114114
// will stick
115115
TypeAdapter<T> d = delegate;
116-
return d != null
117-
? d
118-
: (delegate = gson.getDelegateAdapter(skipPastForGetDelegateAdapter, typeToken));
116+
if (d == null) {
117+
d = delegate = gson.getDelegateAdapter(skipPastForGetDelegateAdapter, typeToken);
118+
}
119+
return d;
119120
}
120121

121122
/**

gson/src/main/java/com/google/gson/stream/JsonReader.java

+34-17
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,8 @@ int doPeek() throws IOException {
590590
int c = nextNonWhitespace(true);
591591
switch (c) {
592592
case ']':
593-
return peeked = PEEKED_END_ARRAY;
593+
peeked = PEEKED_END_ARRAY;
594+
return peeked;
594595
case ';':
595596
checkLenient(); // fall-through
596597
case ',':
@@ -605,7 +606,8 @@ int doPeek() throws IOException {
605606
int c = nextNonWhitespace(true);
606607
switch (c) {
607608
case '}':
608-
return peeked = PEEKED_END_OBJECT;
609+
peeked = PEEKED_END_OBJECT;
610+
return peeked;
609611
case ';':
610612
checkLenient(); // fall-through
611613
case ',':
@@ -617,21 +619,25 @@ int doPeek() throws IOException {
617619
int c = nextNonWhitespace(true);
618620
switch (c) {
619621
case '"':
620-
return peeked = PEEKED_DOUBLE_QUOTED_NAME;
622+
peeked = PEEKED_DOUBLE_QUOTED_NAME;
623+
return peeked;
621624
case '\'':
622625
checkLenient();
623-
return peeked = PEEKED_SINGLE_QUOTED_NAME;
626+
peeked = PEEKED_SINGLE_QUOTED_NAME;
627+
return peeked;
624628
case '}':
625629
if (peekStack != JsonScope.NONEMPTY_OBJECT) {
626-
return peeked = PEEKED_END_OBJECT;
630+
peeked = PEEKED_END_OBJECT;
631+
return peeked;
627632
} else {
628633
throw syntaxError("Expected name");
629634
}
630635
default:
631636
checkLenient();
632637
pos--; // Don't consume the first character in an unquoted string.
633638
if (isLiteral((char) c)) {
634-
return peeked = PEEKED_UNQUOTED_NAME;
639+
peeked = PEEKED_UNQUOTED_NAME;
640+
return peeked;
635641
} else {
636642
throw syntaxError("Expected name");
637643
}
@@ -660,7 +666,8 @@ int doPeek() throws IOException {
660666
} else if (peekStack == JsonScope.NONEMPTY_DOCUMENT) {
661667
int c = nextNonWhitespace(false);
662668
if (c == -1) {
663-
return peeked = PEEKED_EOF;
669+
peeked = PEEKED_EOF;
670+
return peeked;
664671
} else {
665672
checkLenient();
666673
pos--;
@@ -673,7 +680,8 @@ int doPeek() throws IOException {
673680
switch (c) {
674681
case ']':
675682
if (peekStack == JsonScope.EMPTY_ARRAY) {
676-
return peeked = PEEKED_END_ARRAY;
683+
peeked = PEEKED_END_ARRAY;
684+
return peeked;
677685
}
678686
// fall-through to handle ",]"
679687
case ';':
@@ -682,19 +690,24 @@ int doPeek() throws IOException {
682690
if (peekStack == JsonScope.EMPTY_ARRAY || peekStack == JsonScope.NONEMPTY_ARRAY) {
683691
checkLenient();
684692
pos--;
685-
return peeked = PEEKED_NULL;
693+
peeked = PEEKED_NULL;
694+
return peeked;
686695
} else {
687696
throw syntaxError("Unexpected value");
688697
}
689698
case '\'':
690699
checkLenient();
691-
return peeked = PEEKED_SINGLE_QUOTED;
700+
peeked = PEEKED_SINGLE_QUOTED;
701+
return peeked;
692702
case '"':
693-
return peeked = PEEKED_DOUBLE_QUOTED;
703+
peeked = PEEKED_DOUBLE_QUOTED;
704+
return peeked;
694705
case '[':
695-
return peeked = PEEKED_BEGIN_ARRAY;
706+
peeked = PEEKED_BEGIN_ARRAY;
707+
return peeked;
696708
case '{':
697-
return peeked = PEEKED_BEGIN_OBJECT;
709+
peeked = PEEKED_BEGIN_OBJECT;
710+
return peeked;
698711
default:
699712
pos--; // Don't consume the first character in a literal value.
700713
}
@@ -714,7 +727,8 @@ int doPeek() throws IOException {
714727
}
715728

716729
checkLenient();
717-
return peeked = PEEKED_UNQUOTED;
730+
peeked = PEEKED_UNQUOTED;
731+
return peeked;
718732
}
719733

720734
private int peekKeyword() throws IOException {
@@ -763,7 +777,8 @@ private int peekKeyword() throws IOException {
763777

764778
// We've found the keyword followed either by EOF or by a non-literal character.
765779
pos += length;
766-
return peeked = peeking;
780+
peeked = peeking;
781+
return peeked;
767782
}
768783

769784
private int peekNumber() throws IOException {
@@ -865,12 +880,14 @@ private int peekNumber() throws IOException {
865880
&& (value != 0 || !negative)) {
866881
peekedLong = negative ? value : -value;
867882
pos += i;
868-
return peeked = PEEKED_LONG;
883+
peeked = PEEKED_LONG;
884+
return peeked;
869885
} else if (last == NUMBER_CHAR_DIGIT
870886
|| last == NUMBER_CHAR_FRACTION_DIGIT
871887
|| last == NUMBER_CHAR_EXP_DIGIT) {
872888
peekedNumberLength = i;
873-
return peeked = PEEKED_NUMBER;
889+
peeked = PEEKED_NUMBER;
890+
return peeked;
874891
} else {
875892
return PEEKED_NONE;
876893
}

gson/src/test/java/com/google/gson/functional/Java17RecordTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public void testSerializedNameOnAccessor() {
9090
record LocalRecord(int i) {
9191
@SerializedName("a")
9292
@Override
93+
@SuppressWarnings("UnusedMethod")
9394
public int i() {
9495
return i;
9596
}
@@ -170,6 +171,7 @@ record LocalRecord(String s) {
170171
public void testAccessorIsCalled() {
171172
record LocalRecord(String s) {
172173
@Override
174+
@SuppressWarnings("UnusedMethod")
173175
public String s() {
174176
return "accessor-value";
175177
}
@@ -186,6 +188,7 @@ record LocalRecord(String s) {
186188
static final RuntimeException thrownException = new RuntimeException("Custom exception");
187189

188190
@Override
191+
@SuppressWarnings("UnusedMethod")
189192
public String s() {
190193
throw thrownException;
191194
}

pom.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,13 @@
265265
-Xep:NonCanonicalStaticMemberImport
266266
-Xep:NonFinalStaticField
267267
-Xep:PackageLocation:WARN
268+
-Xep:PatternMatchingInstanceof:OFF <!-- disabled: requires Java 16 -->
268269
-Xep:PrimitiveArrayPassedToVarargsMethod
269270
-Xep:PrivateConstructorForUtilityClass:WARN
270271
-Xep:RemoveUnusedImports:WARN
271272
-Xep:StatementSwitchToExpressionSwitch:OFF <!-- disabled: requires Java 14 -->
272273
-Xep:StaticQualifiedUsingExpression <!-- required by style guide -->
274+
-Xep:StringConcatToTextBlock:OFF <!-- disabled: requires Java 15 -->
273275
-Xep:SwitchDefault:WARN
274276
-Xep:SystemExitOutsideMain
275277
-Xep:SystemOut
@@ -300,7 +302,7 @@
300302
<path>
301303
<groupId>com.google.errorprone</groupId>
302304
<artifactId>error_prone_core</artifactId>
303-
<version>2.36.0</version>
305+
<version>2.37.0</version>
304306
</path>
305307
</annotationProcessorPaths>
306308
</configuration>

test-jpms/src/test/java/com/google/gson/jpms_test/ExportedPackagesTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void testMainPackage() {
4949
public void testAnnotationsPackage() throws Exception {
5050
class Annotated {
5151
@SerializedName("custom-name")
52+
@SuppressWarnings("UnusedVariable")
5253
int i;
5354
}
5455

0 commit comments

Comments
 (0)