Skip to content

Commit

Permalink
Add @CheckReturnValue to our packages.
Browse files Browse the repository at this point in the history
...as threatened in
google#2676 (comment).
  • Loading branch information
cpovirk committed May 31, 2024
1 parent 6ebbc9d commit bea052c
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class PostConstructAdapterFactoryTest {
public void test() throws Exception {
Gson gson =
new GsonBuilder().registerTypeAdapterFactory(new PostConstructAdapterFactory()).create();
gson.fromJson("{\"bread\": \"white\", \"cheese\": \"cheddar\"}", Sandwich.class);
Sandwich unused =
gson.fromJson("{\"bread\": \"white\", \"cheese\": \"cheddar\"}", Sandwich.class);
try {
gson.fromJson("{\"bread\": \"cheesey bread\", \"cheese\": \"swiss\"}", Sandwich.class);
fail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
*
* @author Inderjeet Singh, Joel Leitch
*/
@com.google.errorprone.annotations.CheckReturnValue
package com.google.gson.annotations;
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
*
* @author Inderjeet Singh, Joel Leitch, Jesse Wilson
*/
@com.google.errorprone.annotations.CheckReturnValue
package com.google.gson.internal;
1 change: 1 addition & 0 deletions gson/src/main/java/com/google/gson/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
*
* @author Inderjeet Singh, Joel Leitch
*/
@com.google.errorprone.annotations.CheckReturnValue
package com.google.gson;
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
*
* @author Inderjeet Singh, Joel Leitch
*/
@com.google.errorprone.annotations.CheckReturnValue
package com.google.gson.reflect;
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
*/

/** This package provides classes for processing JSON in an efficient streaming way. */
@com.google.errorprone.annotations.CheckReturnValue
package com.google.gson.stream;
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public void testDelegateInvoked() {
bags.add(new BagOfPrimitives(i, i, i % 2 == 0, String.valueOf(i)));
}
String json = gson.toJson(bags);
gson.fromJson(json, new TypeToken<List<BagOfPrimitives>>() {}.getType());
List<BagOfPrimitives> unused =
gson.fromJson(json, new TypeToken<List<BagOfPrimitives>>() {}.getType());
// 11: 1 list object, and 10 entries. stats invoked on all 5 fields
assertThat(stats.numReads).isEqualTo(51);
assertThat(stats.numWrites).isEqualTo(51);
Expand All @@ -64,7 +65,7 @@ public void testDelegateInvoked() {
public void testDelegateInvokedOnStrings() {
String[] bags = {"1", "2", "3", "4"};
String json = gson.toJson(bags);
gson.fromJson(json, String[].class);
String[] unused = gson.fromJson(json, String[].class);
// 1 array object with 4 elements.
assertThat(stats.numReads).isEqualTo(5);
assertThat(stats.numWrites).isEqualTo(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void write(JsonWriter out, A value) throws IOException {

@Override
public A read(JsonReader in) throws IOException {
in.nextString();
String unused = in.nextString();
return new A("jsonAdapter");
}
}
Expand All @@ -277,7 +277,7 @@ public void write(JsonWriter out, T value) throws IOException {
@SuppressWarnings("unchecked")
@Override
public T read(JsonReader in) throws IOException {
in.nextString();
String unused = in.nextString();
return (T) new C("jsonAdapterFactory");
}
};
Expand Down Expand Up @@ -329,7 +329,7 @@ public void write(JsonWriter out, User user) throws IOException {
public User read(JsonReader in) throws IOException {
// implement read: split name into firstName and lastName
in.beginObject();
in.nextName();
String unused = in.nextName();
List<String> nameParts = Splitter.on(" ").splitToList(in.nextString());
in.endObject();
return new User(nameParts.get(0), nameParts.get(1));
Expand All @@ -348,7 +348,7 @@ public void write(JsonWriter out, NullableClass value) throws IOException {

@Override
public NullableClass read(JsonReader in) throws IOException {
in.nextString();
String unused = in.nextString();
return new NullableClass();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public Object read(JsonReader in) throws IOException {
private static JsonIOException assertInaccessibleException(String json, Class<?> toDeserialize) {
Gson gson = new Gson();
try {
gson.fromJson(json, toDeserialize);
Object unused = gson.fromJson(json, toDeserialize);
throw new AssertionError(
"Missing exception; test has to be run with `--illegal-access=deny`");
} catch (JsonSyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class SubSubTypeToken2 extends SubTypeToken<Integer> {}
}

private static <M> void createTypeTokenTypeVariable() {
new TypeToken<M>() {};
TypeToken<M> unused = new TypeToken<M>() {};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void setUp() throws Exception {
/** Benchmark to measure Gson performance for deserializing an object */
public void timeBagOfPrimitivesDefault(int reps) {
for (int i = 0; i < reps; ++i) {
gson.fromJson(json, BagOfPrimitives.class);
BagOfPrimitives unused = gson.fromJson(json, BagOfPrimitives.class);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void setUp() throws Exception {
/** Benchmark to measure Gson performance for deserializing an object */
public void timeCollectionsDefault(int reps) {
for (int i = 0; i < reps; ++i) {
gson.fromJson(json, LIST_TYPE_TOKEN);
List<BagOfPrimitives> unused = gson.fromJson(json, LIST_TYPE_TOKEN);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.caliper.Param;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -195,19 +196,19 @@ private static void readToken(JsonReader reader) throws IOException {
reader.endObject();
break;
case NAME:
reader.nextName();
String unusedName = reader.nextName();
break;
case BOOLEAN:
reader.nextBoolean();
boolean unusedBoolean = reader.nextBoolean();
break;
case NULL:
reader.nextNull();
break;
case NUMBER:
reader.nextLong();
long unusedLong = reader.nextLong();
break;
case STRING:
reader.nextString();
String unusedString = reader.nextString();
break;
case END_DOCUMENT:
return;
Expand Down Expand Up @@ -274,7 +275,7 @@ public void parse(char[] data, Document document) throws Exception {
private static class GsonDomParser implements Parser {
@Override
public void parse(char[] data, Document document) throws Exception {
JsonParser.parseReader(new CharArrayReader(data));
JsonElement unused = JsonParser.parseReader(new CharArrayReader(data));
}
}

Expand All @@ -284,7 +285,7 @@ private static class GsonBindParser implements Parser {

@Override
public void parse(char[] data, Document document) throws Exception {
gson.fromJson(new CharArrayReader(data), document.gsonType);
Object unused = gson.fromJson(new CharArrayReader(data), document.gsonType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void setUp() throws Exception {

public void timeObjectSerialization(int reps) {
for (int i = 0; i < reps; ++i) {
gson.toJson(bag);
String unused = gson.toJson(bag);
}
}
}

0 comments on commit bea052c

Please sign in to comment.