Skip to content

Commit 523694a

Browse files
eamonnmcmanustibor-universe
authored andcommitted
Add @CanIgnoreReturnValue as appropriate to Gson methods. (google#2369)
This annotation indicates that return value of the annotated method does not need to be used. If it is _not_ present on a non-void method, and if Error Prone's `CheckReturnValue` is active, then calling the method without using the result is an error. However, we are not enabling `CheckReturnValue` by default here. Also update some code that does ignore return values, so that the returned value is used, if only by assigning it to an unused variable.
1 parent 3cd8141 commit 523694a

32 files changed

+245
-190
lines changed

extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gson.typeadapters;
1818

19+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1920
import com.google.gson.Gson;
2021
import com.google.gson.JsonElement;
2122
import com.google.gson.JsonObject;
@@ -179,6 +180,7 @@ public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType) {
179180
* Ensures that this factory will handle not just the given {@code baseType}, but any subtype
180181
* of that type.
181182
*/
183+
@CanIgnoreReturnValue
182184
public RuntimeTypeAdapterFactory<T> recognizeSubtypes() {
183185
this.recognizeSubtypes = true;
184186
return this;
@@ -191,6 +193,7 @@ public RuntimeTypeAdapterFactory<T> recognizeSubtypes() {
191193
* @throws IllegalArgumentException if either {@code type} or {@code label}
192194
* have already been registered on this type adapter.
193195
*/
196+
@CanIgnoreReturnValue
194197
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) {
195198
if (type == null || label == null) {
196199
throw new NullPointerException();
@@ -210,6 +213,7 @@ public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, Str
210213
* @throws IllegalArgumentException if either {@code type} or its simple name
211214
* have already been registered on this type adapter.
212215
*/
216+
@CanIgnoreReturnValue
213217
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
214218
return registerSubtype(type, type.getSimpleName());
215219
}

extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public void testDeserializeListOfLists() {
129129
@Test
130130
public void testSerializationWithMultipleTypes() {
131131
Company google = new Company("Google");
132-
new Employee("Jesse", google);
133-
new Employee("Joel", google);
132+
Employee unused1 = new Employee("Jesse", google);
133+
Employee unused2 = new Employee("Joel", google);
134134

135135
GsonBuilder gsonBuilder = new GsonBuilder();
136136
new GraphAdapterBuilder()

extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ public void testCustomTypeAdapter() {
110110

111111
@Override public User read(JsonReader in) throws IOException {
112112
in.beginObject();
113-
in.nextName();
113+
String unused1 = in.nextName();
114114
String name = in.nextString();
115-
in.nextName();
115+
String unused2 = in.nextName();
116116
String password = in.nextString();
117117
in.endObject();
118118
return new User(name, password);

extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testUtcDatesOnJdkBefore1_7() {
6464
Gson gson = new GsonBuilder()
6565
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
6666
.create();
67-
gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class);
67+
Date unused = gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class);
6868
}
6969

7070
@Test

gson/src/main/java/com/google/gson/Gson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOExce
12311231
boolean oldLenient = reader.isLenient();
12321232
reader.setLenient(true);
12331233
try {
1234-
reader.peek();
1234+
JsonToken unused = reader.peek();
12351235
isEmpty = false;
12361236
TypeAdapter<T> typeAdapter = getAdapter(typeOfT);
12371237
return typeAdapter.read(reader);

gson/src/main/java/com/google/gson/GsonBuilder.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static com.google.gson.Gson.DEFAULT_SPECIALIZE_FLOAT_VALUES;
2929
import static com.google.gson.Gson.DEFAULT_USE_JDK_UNSAFE;
3030

31+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
3132
import com.google.gson.annotations.Since;
3233
import com.google.gson.annotations.Until;
3334
import com.google.gson.internal.$Gson$Preconditions;
@@ -159,6 +160,7 @@ public GsonBuilder() {
159160
* @see Since
160161
* @see Until
161162
*/
163+
@CanIgnoreReturnValue
162164
public GsonBuilder setVersion(double version) {
163165
if (Double.isNaN(version) || version < 0.0) {
164166
throw new IllegalArgumentException("Invalid version: " + version);
@@ -181,6 +183,7 @@ public GsonBuilder setVersion(double version) {
181183
* {@link java.lang.reflect.Modifier#STATIC}.
182184
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
183185
*/
186+
@CanIgnoreReturnValue
184187
public GsonBuilder excludeFieldsWithModifiers(int... modifiers) {
185188
Objects.requireNonNull(modifiers);
186189
excluder = excluder.withModifiers(modifiers);
@@ -196,6 +199,7 @@ public GsonBuilder excludeFieldsWithModifiers(int... modifiers) {
196199
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
197200
* @since 1.3
198201
*/
202+
@CanIgnoreReturnValue
199203
public GsonBuilder generateNonExecutableJson() {
200204
this.generateNonExecutableJson = true;
201205
return this;
@@ -210,6 +214,7 @@ public GsonBuilder generateNonExecutableJson() {
210214
*
211215
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
212216
*/
217+
@CanIgnoreReturnValue
213218
public GsonBuilder excludeFieldsWithoutExposeAnnotation() {
214219
excluder = excluder.excludeFieldsWithoutExposeAnnotation();
215220
return this;
@@ -222,6 +227,7 @@ public GsonBuilder excludeFieldsWithoutExposeAnnotation() {
222227
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
223228
* @since 1.2
224229
*/
230+
@CanIgnoreReturnValue
225231
public GsonBuilder serializeNulls() {
226232
this.serializeNulls = true;
227233
return this;
@@ -306,6 +312,7 @@ public GsonBuilder serializeNulls() {
306312
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
307313
* @since 1.7
308314
*/
315+
@CanIgnoreReturnValue
309316
public GsonBuilder enableComplexMapKeySerialization() {
310317
complexMapKeySerialization = true;
311318
return this;
@@ -330,6 +337,7 @@ public GsonBuilder enableComplexMapKeySerialization() {
330337
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
331338
* @since 1.3
332339
*/
340+
@CanIgnoreReturnValue
333341
public GsonBuilder disableInnerClassSerialization() {
334342
excluder = excluder.disableInnerClassSerialization();
335343
return this;
@@ -343,6 +351,7 @@ public GsonBuilder disableInnerClassSerialization() {
343351
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
344352
* @since 1.3
345353
*/
354+
@CanIgnoreReturnValue
346355
public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) {
347356
this.longSerializationPolicy = Objects.requireNonNull(serializationPolicy);
348357
return this;
@@ -354,6 +363,7 @@ public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializat
354363
*
355364
* <p>This method just delegates to {@link #setFieldNamingStrategy(FieldNamingStrategy)}.
356365
*/
366+
@CanIgnoreReturnValue
357367
public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) {
358368
return setFieldNamingStrategy(namingConvention);
359369
}
@@ -370,6 +380,7 @@ public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) {
370380
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
371381
* @since 1.3
372382
*/
383+
@CanIgnoreReturnValue
373384
public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) {
374385
this.fieldNamingPolicy = Objects.requireNonNull(fieldNamingStrategy);
375386
return this;
@@ -383,6 +394,7 @@ public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrateg
383394
* @see ToNumberPolicy#DOUBLE The default object-to-number strategy
384395
* @since 2.8.9
385396
*/
397+
@CanIgnoreReturnValue
386398
public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStrategy) {
387399
this.objectToNumberStrategy = Objects.requireNonNull(objectToNumberStrategy);
388400
return this;
@@ -396,6 +408,7 @@ public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStra
396408
* @see ToNumberPolicy#LAZILY_PARSED_NUMBER The default number-to-number strategy
397409
* @since 2.8.9
398410
*/
411+
@CanIgnoreReturnValue
399412
public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStrategy) {
400413
this.numberToNumberStrategy = Objects.requireNonNull(numberToNumberStrategy);
401414
return this;
@@ -427,6 +440,7 @@ public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStra
427440
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
428441
* @since 1.4
429442
*/
443+
@CanIgnoreReturnValue
430444
public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) {
431445
Objects.requireNonNull(strategies);
432446
for (ExclusionStrategy strategy : strategies) {
@@ -450,6 +464,7 @@ public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) {
450464
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
451465
* @since 1.7
452466
*/
467+
@CanIgnoreReturnValue
453468
public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy) {
454469
Objects.requireNonNull(strategy);
455470
excluder = excluder.withExclusionStrategy(strategy, true, false);
@@ -471,6 +486,7 @@ public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy)
471486
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
472487
* @since 1.7
473488
*/
489+
@CanIgnoreReturnValue
474490
public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) {
475491
Objects.requireNonNull(strategy);
476492
excluder = excluder.withExclusionStrategy(strategy, false, true);
@@ -486,6 +502,7 @@ public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strateg
486502
*
487503
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
488504
*/
505+
@CanIgnoreReturnValue
489506
public GsonBuilder setPrettyPrinting() {
490507
return setPrettyPrinting(FormattingStyle.DEFAULT);
491508
}
@@ -500,6 +517,7 @@ public GsonBuilder setPrettyPrinting() {
500517
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
501518
* @since $next-version$
502519
*/
520+
@CanIgnoreReturnValue
503521
public GsonBuilder setPrettyPrinting(FormattingStyle formattingStyle) {
504522
this.formattingStyle = formattingStyle;
505523
return this;
@@ -515,6 +533,7 @@ public GsonBuilder setPrettyPrinting(FormattingStyle formattingStyle) {
515533
* @see JsonReader#setLenient(boolean)
516534
* @see JsonWriter#setLenient(boolean)
517535
*/
536+
@CanIgnoreReturnValue
518537
public GsonBuilder setLenient() {
519538
lenient = true;
520539
return this;
@@ -527,6 +546,7 @@ public GsonBuilder setLenient() {
527546
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
528547
* @since 1.3
529548
*/
549+
@CanIgnoreReturnValue
530550
public GsonBuilder disableHtmlEscaping() {
531551
this.escapeHtmlChars = false;
532552
return this;
@@ -548,6 +568,7 @@ public GsonBuilder disableHtmlEscaping() {
548568
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
549569
* @since 1.2
550570
*/
571+
@CanIgnoreReturnValue
551572
public GsonBuilder setDateFormat(String pattern) {
552573
// TODO(Joel): Make this fail fast if it is an invalid date format
553574
this.datePattern = pattern;
@@ -568,6 +589,7 @@ public GsonBuilder setDateFormat(String pattern) {
568589
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
569590
* @since 1.2
570591
*/
592+
@CanIgnoreReturnValue
571593
public GsonBuilder setDateFormat(int style) {
572594
this.dateStyle = style;
573595
this.datePattern = null;
@@ -589,6 +611,7 @@ public GsonBuilder setDateFormat(int style) {
589611
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
590612
* @since 1.2
591613
*/
614+
@CanIgnoreReturnValue
592615
public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
593616
this.dateStyle = dateStyle;
594617
this.timeStyle = timeStyle;
@@ -618,6 +641,7 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
618641
* {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
619642
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
620643
*/
644+
@CanIgnoreReturnValue
621645
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
622646
Objects.requireNonNull(type);
623647
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
@@ -651,6 +675,7 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
651675
*
652676
* @since 2.1
653677
*/
678+
@CanIgnoreReturnValue
654679
public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
655680
Objects.requireNonNull(factory);
656681
factories.add(factory);
@@ -671,6 +696,7 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
671696
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
672697
* @since 1.7
673698
*/
699+
@CanIgnoreReturnValue
674700
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
675701
Objects.requireNonNull(baseType);
676702
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
@@ -707,6 +733,7 @@ public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAd
707733
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
708734
* @since 1.3
709735
*/
736+
@CanIgnoreReturnValue
710737
public GsonBuilder serializeSpecialFloatingPointValues() {
711738
this.serializeSpecialFloatingPointValues = true;
712739
return this;
@@ -728,6 +755,7 @@ public GsonBuilder serializeSpecialFloatingPointValues() {
728755
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
729756
* @since 2.9.0
730757
*/
758+
@CanIgnoreReturnValue
731759
public GsonBuilder disableJdkUnsafe() {
732760
this.useJdkUnsafe = false;
733761
return this;
@@ -753,6 +781,7 @@ public GsonBuilder disableJdkUnsafe() {
753781
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
754782
* @since 2.9.1
755783
*/
784+
@CanIgnoreReturnValue
756785
public GsonBuilder addReflectionAccessFilter(ReflectionAccessFilter filter) {
757786
Objects.requireNonNull(filter);
758787
reflectionFilters.addFirst(filter);

gson/src/main/java/com/google/gson/JsonArray.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gson;
1818

19+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1920
import com.google.gson.internal.NonNullElementWrapperList;
2021
import java.math.BigDecimal;
2122
import java.math.BigInteger;
@@ -145,6 +146,7 @@ public void addAll(JsonArray array) {
145146
* @return the element previously at the specified position
146147
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds
147148
*/
149+
@CanIgnoreReturnValue
148150
public JsonElement set(int index, JsonElement element) {
149151
return elements.set(index, element == null ? JsonNull.INSTANCE : element);
150152
}
@@ -157,6 +159,7 @@ public JsonElement set(int index, JsonElement element) {
157159
* @return true if this array contained the specified element, false otherwise
158160
* @since 2.3
159161
*/
162+
@CanIgnoreReturnValue
160163
public boolean remove(JsonElement element) {
161164
return elements.remove(element);
162165
}
@@ -171,6 +174,7 @@ public boolean remove(JsonElement element) {
171174
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds
172175
* @since 2.3
173176
*/
177+
@CanIgnoreReturnValue
174178
public JsonElement remove(int index) {
175179
return elements.remove(index);
176180
}

gson/src/main/java/com/google/gson/JsonElement.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gson;
1818

19+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1920
import com.google.gson.internal.Streams;
2021
import com.google.gson.stream.JsonWriter;
2122
import java.io.IOException;
@@ -143,6 +144,7 @@ public JsonPrimitive getAsJsonPrimitive() {
143144
* @throws IllegalStateException if this element is of another type.
144145
* @since 1.2
145146
*/
147+
@CanIgnoreReturnValue
146148
public JsonNull getAsJsonNull() {
147149
if (isJsonNull()) {
148150
return (JsonNull) this;

gson/src/main/java/com/google/gson/JsonObject.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gson;
1818

19+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1920
import com.google.gson.internal.LinkedTreeMap;
2021
import java.util.Map;
2122
import java.util.Set;
@@ -77,6 +78,7 @@ public void add(String property, JsonElement value) {
7778
* member with this name exists.
7879
* @since 1.3
7980
*/
81+
@CanIgnoreReturnValue
8082
public JsonElement remove(String property) {
8183
return members.remove(property);
8284
}

gson/src/main/java/com/google/gson/JsonStreamParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package com.google.gson;
2020

21+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2122
import com.google.gson.internal.Streams;
2223
import com.google.gson.stream.JsonReader;
2324
import com.google.gson.stream.JsonToken;

0 commit comments

Comments
 (0)