Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error prone warns #2320

Merged
merged 57 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
18336d0
Adds `@SuppressWarnings("NarrowingCompoundAssignment")`
MaicolAntali Feb 18, 2023
0c2f8c3
Adds `@SuppressWarnings("TypeParameterUnusedInFormals")`
MaicolAntali Feb 18, 2023
7f64bf3
Adds `@SuppressWarnings("JavaUtilDate")`
MaicolAntali Feb 18, 2023
f34c35d
Adds a limit to `String.split()`
MaicolAntali Feb 18, 2023
5e6a279
Add `error_prone_annotations` to the `pom.xml`
MaicolAntali Feb 18, 2023
682b369
Adds `@InlineMe(...)` to deprecated methods
MaicolAntali Feb 18, 2023
41e0c28
Adds `@SuppressWarnings("ImmutableEnumChecker")`
MaicolAntali Feb 18, 2023
b6965d1
Adds `@SuppressWarnings("ModifiedButNotUsed")`
MaicolAntali Feb 18, 2023
04feff6
Adds `@SuppressWarnings("MixedMutabilityReturnType")`
MaicolAntali Feb 18, 2023
5bd180d
Removes an unused import
MaicolAntali Feb 18, 2023
373525d
Adds `requires` to `module-info.java`
MaicolAntali Feb 19, 2023
bd60abf
Adds ErrorProne `link` into `pom.xml`
MaicolAntali Feb 19, 2023
5e1e970
Remove unused imports
MaicolAntali Feb 19, 2023
f3c4763
Adds `@SuppressWarnings("EqualsGetClass")`
MaicolAntali Feb 19, 2023
71a75b8
Excludes from `proto` just the generated code.
MaicolAntali Feb 19, 2023
a1beaf2
Removes an unused variable
MaicolAntali Feb 19, 2023
9c5073f
Fixes the `BadImport` warn into `ProtosWithAnnotationsTest`
MaicolAntali Feb 19, 2023
2aad0bc
Fixes the `BadImport` warns into `ProtosWithAnnotationsTest`
MaicolAntali Feb 19, 2023
ba27715
Enables ErrorProne in `gson/src/test.*`
MaicolAntali Feb 19, 2023
e3d65bc
Fixes `UnusedVariable` warns
MaicolAntali Feb 19, 2023
d1c0554
Fixes `JavaUtilDate` warns
MaicolAntali Feb 19, 2023
7769c75
Fixes `EqualsGetClass` warns
MaicolAntali Feb 19, 2023
a8cff33
Replaces pattern matching for instanceof with casts
MaicolAntali Feb 19, 2023
eb05e0d
Fixes `JdkObsolete` warns
MaicolAntali Feb 22, 2023
ef27eb4
Fixes `ClassCanBeStatic` warns
MaicolAntali Feb 22, 2023
69b34be
Fixes `UndefinedEquals` warns
MaicolAntali Feb 22, 2023
7a20385
Fixes `GetClassOnEnum` warns
MaicolAntali Feb 22, 2023
4627307
Fixes `ImmutableEnumChecker` warns
MaicolAntali Feb 22, 2023
13de98d
Fixes `StaticAssignmentOfThrowable` warns
MaicolAntali Feb 22, 2023
3ae4fac
Fixes `AssertionFailureIgnored` warns
MaicolAntali Feb 22, 2023
661eae8
Fixes `ModifiedButNotUsed` warns
MaicolAntali Feb 22, 2023
2742881
Fixes `MissingSummary` warns
MaicolAntali Feb 22, 2023
7d688ed
Fixes `FloatingPointLiteralPrecision` warns
MaicolAntali Feb 22, 2023
67a8edf
Fixes `StringSplitter` warns
MaicolAntali Feb 22, 2023
b92cb96
Fixes `EmptyCatch` warns
MaicolAntali Feb 22, 2023
dcb277e
Fixes `UnicodeEscape` warns
MaicolAntali Feb 22, 2023
6dee398
Fixes `EmptyBlockTag` warns
MaicolAntali Feb 22, 2023
682d5df
Fixes `LongFloatConversion` warns
MaicolAntali Feb 22, 2023
c1681bf
Fixes `LongDoubleConversion` warns
MaicolAntali Feb 22, 2023
69dcd03
Fixes `TruthAssertExpected` warns
MaicolAntali Feb 22, 2023
03e86d6
Fixes `UnusedMethod` warns
MaicolAntali Feb 22, 2023
1feefb4
Fixes `UnusedTypeParameter` warns
MaicolAntali Feb 22, 2023
7ac4465
Fixes `CatchFail` warns
MaicolAntali Feb 22, 2023
92972bf
Fixes `MathAbsoluteNegative` warns
MaicolAntali Feb 22, 2023
2167d4c
Fixes `LoopOverCharArray` warns
MaicolAntali Feb 22, 2023
126b1b5
Fixes `HidingField` warns
MaicolAntali Feb 22, 2023
4aaefea
Implements code review feedback
MaicolAntali Feb 22, 2023
f1c5591
Implements code review feedback
MaicolAntali Feb 22, 2023
7590168
Enable ErrorProne in `extra`
MaicolAntali Feb 22, 2023
1bb54c4
Fix the `JavaUtilDate` warns
MaicolAntali Feb 22, 2023
a507512
Implements code review feedback
MaicolAntali Feb 23, 2023
bd2916e
Removes redundant new-line
MaicolAntali Feb 23, 2023
13ab165
Implements code review feedback
MaicolAntali Feb 24, 2023
a7cca38
Adds `JDK11` to run test with `--release 11`
MaicolAntali Feb 24, 2023
5dbbbee
Revert "Adds `JDK11` to run test with `--release 11`"
MaicolAntali Feb 24, 2023
f89d42a
Merge branch 'master' into fix-error-prone-warns
MaicolAntali Feb 28, 2023
039bf62
Merge branch 'master' into fix-error-prone-warns
MaicolAntali Mar 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.TimeZone;
import org.junit.Test;

@SuppressWarnings("JavaUtilDate")
public final class UtcDateTypeAdapterTest {
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
Expand Down
1 change: 1 addition & 0 deletions gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
<version>2.18.0</version>
<optional>true</optional>
</dependency>
</dependencies>

Expand Down
6 changes: 3 additions & 3 deletions gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ public String getPath() {
* @throws NumberFormatException if any unicode escape sequences are
* malformed.
*/
@SuppressWarnings({"fallthrough", "NarrowingCompoundAssignment"})
@SuppressWarnings("fallthrough")
private char readEscapeCharacter() throws IOException {
if (pos == limit && !fillBuffer(1)) {
throw syntaxError("Unterminated escape sequence");
Expand All @@ -1603,7 +1603,7 @@ private char readEscapeCharacter() throws IOException {
throw syntaxError("Unterminated escape sequence");
}
// Equivalent to Integer.parseInt(stringPool.get(buffer, pos, 4), 16);
char result = 0;
int result = 0;
for (int i = pos, end = i + 4; i < end; i++) {
char c = buffer[i];
result <<= 4;
Expand All @@ -1618,7 +1618,7 @@ private char readEscapeCharacter() throws IOException {
}
}
pos += 4;
return result;
return (char) result;

case 't':
return '\t';
Expand Down
2 changes: 1 addition & 1 deletion gson/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
exports com.google.gson.stream;

// Dependency on Error Prone Annotations
requires com.google.errorprone.annotations;
requires static com.google.errorprone.annotations;

// Optional dependency on java.sql
requires static java.sql;
Expand Down
2 changes: 2 additions & 0 deletions gson/src/test/java/com/google/gson/CommentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.junit.Test;

/**
* Tests that by default Gson accepts several forms of comments.
*
* @author Jesse Wilson
*/
public final class CommentsTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void testIncludeStaticNestedClassField() throws Exception {
assertThat(excluder.excludeField(f, true)).isFalse();
}

@SuppressWarnings("ClassCanBeStatic")
class InnerClass {
}

Expand Down
2 changes: 2 additions & 0 deletions gson/src/test/java/com/google/gson/JsonArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.junit.Test;

/**
* Tests handling of JSON arrays.
*
* @author Jesse Wilson
*/
public final class JsonArrayTest {
Expand Down
2 changes: 2 additions & 0 deletions gson/src/test/java/com/google/gson/JsonNullTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.junit.Test;

/**
* Tests handling of JSON nulls.
*
* @author Jesse Wilson
*/
public final class JsonNullTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testDeserializeDeeplyNestedObjects() throws IOException {
assertThat(actualTimes).isEqualTo(times);
}

@SuppressWarnings("unused")
@SuppressWarnings({"unused", "ClassCanBeStatic"})
private class RuntimeType {
Object a = 5;
Object b = Arrays.asList(1, 2, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.junit.Test;

/**
* Tests handling of Core Type Adapters
*
* @author Jesse Wilson
*/
public class OverrideCoreTypeAdaptersTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
public class ParameterizedTypeFixtures {

public static class MyParameterizedType<T> {
public static final class MyParameterizedType<T> {
public final T value;
public MyParameterizedType(T value) {
this.value = value;
Expand Down Expand Up @@ -103,7 +103,7 @@ public static class MyParameterizedTypeInstanceCreator<T>
* This is usually fine in tests since there we deserialize just once, but quite
* dangerous in practice.
*
* @param instanceOfT
* @param instanceOfT Instance of Type
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
*/
public MyParameterizedTypeInstanceCreator(T instanceOfT) {
this.instanceOfT = instanceOfT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void testTopLevelListOfIntegerCollectionsDeserialization() {
}

@Test
@SuppressWarnings("JdkObsolete")
public void testLinkedListSerialization() {
List<String> list = new LinkedList<>();
list.add("a1");
Expand All @@ -113,6 +114,7 @@ public void testLinkedListDeserialization() {
}

@Test
@SuppressWarnings("JdkObsolete")
public void testQueueSerialization() {
Queue<String> queue = new LinkedList<>();
queue.add("a1");
Expand Down Expand Up @@ -266,11 +268,11 @@ public void testObjectCollectionSerialization() {
}

@Test
public void testRawCollectionDeserializationNotAlllowed() {
public void testRawCollectionDeserializationNotAllowed() {
String json = "[0,1,2,3,4,5,6,7,8,9]";
Collection<?> integers = gson.fromJson(json, Collection.class);
// JsonReader converts numbers to double by default so we need a floating point comparison
assertThat(integers).isEqualTo(Arrays.asList(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0));
assertThat(integers).containsExactly(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0).inOrder();

json = "[\"Hello\", \"World\"]";
Collection<?> strings = gson.fromJson(json, Collection.class);
Expand Down Expand Up @@ -416,9 +418,9 @@ public void testSetDeserialization() {
}
}

private class BigClass { private Map<String, ? extends List<SmallClass>> inBig; }
private static class BigClass { private Map<String, ? extends List<SmallClass>> inBig; }
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved

private class SmallClass { private String inSmall; }
private static class SmallClass { private String inSmall; }

@Test
public void testIssue1107() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.errorprone.annotations.Immutable;
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
Expand Down Expand Up @@ -130,6 +131,7 @@ private static class MyBase {
static final String TYPE_ACCESS = "__type__";
}

@SuppressWarnings("ImmutableEnumChecker")
private enum SubTypes {
SUB_TYPE1(SubType1.class),
SUB_TYPE2(SubType2.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.common.base.Splitter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
Expand All @@ -35,6 +36,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Before;
Expand Down Expand Up @@ -291,9 +293,9 @@ private static final class StringHolder {
String part2;

public StringHolder(String string) {
String[] parts = string.split(":");
part1 = parts[0];
part2 = parts[1];
List<String> parts = Splitter.on(':').splitToList(string);
part1 = parts.get(0);
part2 = parts.get(1);
}
public StringHolder(String part1, String part2) {
this.part1 = part1;
Expand Down Expand Up @@ -425,7 +427,7 @@ public void testEnsureCustomDeserializerNotInvokedForNullValues() {

// Test created from Issue 352
@Test
@SuppressWarnings("JavaUtilDate")
@SuppressWarnings({"JavaUtilDate", "UndefinedEquals"})
public void testRegisterHierarchyAdapterForDate() {
Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(Date.class, new DateTypeAdapter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ public void testStringBuilderDeserialization() {
}

@Test
@SuppressWarnings("JdkObsolete")
public void testStringBufferSerialization() {
StringBuffer sb = new StringBuffer("abc");
String json = gson.toJson(sb);
Expand Down
9 changes: 5 additions & 4 deletions gson/src/test/java/com/google/gson/functional/EnumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public String getExpectedJson() {
*/
@Test
public void testEnumSubclass() {
assertThat(Roshambo.ROCK.getClass()).isAssignableTo(Roshambo.class);
assertThat(Roshambo.ROCK.getDeclaringClass()).isAssignableTo(Roshambo.class);
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
assertThat(gson.toJson(Roshambo.ROCK)).isEqualTo("\"ROCK\"");
assertThat(gson.toJson(EnumSet.allOf(Roshambo.class))).isEqualTo("[\"ROCK\",\"PAPER\",\"SCISSORS\"]");
assertThat(gson.fromJson("\"ROCK\"", Roshambo.class)).isEqualTo(Roshambo.ROCK);
Expand All @@ -135,7 +135,7 @@ public void testEnumSubclassWithRegisteredTypeAdapter() {
gson = new GsonBuilder()
.registerTypeHierarchyAdapter(Roshambo.class, new MyEnumTypeAdapter())
.create();
assertThat(Roshambo.ROCK.getClass()).isAssignableTo(Roshambo.class);
assertThat(Roshambo.ROCK.getDeclaringClass()).isAssignableTo(Roshambo.class);
assertThat(gson.toJson(Roshambo.ROCK)).isEqualTo("\"123ROCK\"");
assertThat(gson.toJson(EnumSet.allOf(Roshambo.class))).isEqualTo("[\"123ROCK\",\"123PAPER\",\"123SCISSORS\"]");
assertThat(gson.fromJson("\"123ROCK\"", Roshambo.class)).isEqualTo(Roshambo.ROCK);
Expand Down Expand Up @@ -207,6 +207,7 @@ private enum Roshambo {
}
};

@SuppressWarnings("unused")
abstract Roshambo defeats();
}

Expand Down Expand Up @@ -239,8 +240,8 @@ public void testEnumClassWithFields() {

private enum Color {
RED("red", 1), BLUE("blue", 2), GREEN("green", 3);
String value;
int index;
final String value;
final int index;
private Color(String value, int index) {
this.value = value;
this.index = index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testEscapingObjectFields() {
assertThat(jsonRepresentation).contains("\\\"");

BagOfPrimitives expectedObject = gson.fromJson(jsonRepresentation, BagOfPrimitives.class);
assertThat(expectedObject.getExpectedJson()).isEqualTo(objWithPrimitives.getExpectedJson());
assertThat(objWithPrimitives.getExpectedJson()).isEqualTo(expectedObject.getExpectedJson());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public void testDefaultNestedStaticClassIncluded() {
}

private static class Outer {

@SuppressWarnings("ClassCanBeStatic")
private class Inner extends NestedClass {
public Inner(String value) {
super(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void testVersionPattern() {
}

@Test
@SuppressWarnings("AssertionFailureIgnored")
public void testAssertionErrorInSerializationPrintsVersion() {
try {
gson.toJson(new TestType());
Expand All @@ -69,6 +70,7 @@ public void testAssertionErrorInSerializationPrintsVersion() {
}

@Test
@SuppressWarnings("AssertionFailureIgnored")
public void testAssertionErrorInDeserializationPrintsVersion() {
try {
gson.fromJson("{'a':'abc'}", TestType.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void appendFields(StringBuilder sb) {
}

@Test
@SuppressWarnings("JdkObsolete")
public void testSubInterfacesOfCollectionSerialization() {
List<Integer> list = new LinkedList<>();
list.add(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ record LocalRecord(String s) {

/** Tests behavior when the canonical constructor throws an exception */
@Test
@SuppressWarnings("StaticAssignmentOfThrowable")
public void testThrowingConstructor() {
record LocalRecord(String s) {
static final RuntimeException thrownException = new RuntimeException("Custom exception");
Expand Down Expand Up @@ -172,6 +173,7 @@ public String s() {

/** Tests behavior when a record accessor method throws an exception */
@Test
@SuppressWarnings("StaticAssignmentOfThrowable")
public void testThrowingAccessor() {
record LocalRecord(String s) {
static final RuntimeException thrownException = new RuntimeException("Custom exception");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.common.base.Splitter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
Expand All @@ -36,6 +37,7 @@
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Locale;
import org.junit.Test;

Expand Down Expand Up @@ -226,9 +228,9 @@ private static class UserJsonAdapter extends TypeAdapter<User> {
// implement read: split name into firstName and lastName
in.beginObject();
in.nextName();
String[] nameParts = in.nextString().split(" ");
List<String> nameParts = Splitter.on(" ").splitToList(in.nextString());
in.endObject();
return new User(nameParts[0], nameParts[1]);
return new User(nameParts.get(0), nameParts.get(1));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void testClassWithNoFieldsDeserialization() {
private static class Subclass extends Superclass1 {
}
private static class Superclass1 extends Superclass2 {
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "HidingField"})
String s;
}
private static class Superclass2 {
Expand Down Expand Up @@ -414,6 +414,8 @@ public void testInnerClassDeserialization() {
private static class Parent {
@SuppressWarnings("unused")
int value1 = 1;

@SuppressWarnings("ClassCanBeStatic")
private class Child {
int value2 = 2;
}
Expand Down Expand Up @@ -655,6 +657,7 @@ public void testThrowingDefaultConstructor() {
}
}

@SuppressWarnings("StaticAssignmentOfThrowable")
static class ClassWithThrowingConstructor {
static final RuntimeException thrownException = new RuntimeException("Custom exception");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public String toString(T obj) {
}
}

private static class MultiParameters<A, B, C, D, E> {
private static final class MultiParameters<A, B, C, D, E> {
A a;
B b;
C c;
Expand Down
Loading