Skip to content

Commit 1bec842

Browse files
cushonError Prone Team
authored andcommitted
Fix a crash in UnnecessaryAsync
PiperOrigin-RevId: 567041651
1 parent d2ee28e commit 1bec842

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryAsync.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private SuggestedFix attemptFix(VariableTree tree, VisitorState state) {
142142
getPrimitiveType(symbol.type, state.getTypes()),
143143
symbol.getSimpleName(),
144144
constructor.getArguments().isEmpty()
145-
? getDefaultInitializer(symbol)
145+
? getDefaultInitializer(symbol, state.getTypes())
146146
: state.getSourceForNode(constructor.getArguments().get(0))));
147147

148148
new TreePathScanner<Void, Void>() {
@@ -217,7 +217,8 @@ private boolean isVariableDeclarationItself(Tree parentTree) {
217217
}
218218

219219
private static String getPrimitiveType(Type type, Types types) {
220-
switch (types.erasure(type).toString()) {
220+
String name = types.erasure(type).toString();
221+
switch (name) {
221222
case "java.util.concurrent.atomic.AtomicBoolean":
222223
return "boolean";
223224
case "java.util.concurrent.atomic.AtomicReference":
@@ -228,22 +229,23 @@ private static String getPrimitiveType(Type type, Types types) {
228229
return "int";
229230
case "java.util.concurrent.atomic.AtomicLong":
230231
return "long";
231-
default: // fall out
232+
default:
233+
throw new AssertionError(name);
232234
}
233-
throw new AssertionError();
234235
}
235236

236-
private static String getDefaultInitializer(VarSymbol symbol) {
237-
switch (symbol.type.toString()) {
237+
private static String getDefaultInitializer(VarSymbol symbol, Types types) {
238+
String name = types.erasure(symbol.type).toString();
239+
switch (name) {
238240
case "java.util.concurrent.atomic.AtomicBoolean":
239241
return "false";
240242
case "java.util.concurrent.atomic.AtomicReference":
241243
return "null";
242244
case "java.util.concurrent.atomic.AtomicInteger":
243245
case "java.util.concurrent.atomic.AtomicLong":
244246
return "0";
245-
default: // fall out
247+
default:
248+
throw new AssertionError(name);
246249
}
247-
throw new AssertionError();
248250
}
249251
}

core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryAsyncTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,19 @@ public void negative_capturedByLambda() {
205205
"}")
206206
.doTest();
207207
}
208+
209+
@Test
210+
public void atomicReference_unfixable() {
211+
helper
212+
.addSourceLines(
213+
"Test.java",
214+
"import java.util.concurrent.atomic.AtomicReference;",
215+
"class Test {",
216+
" void f() {",
217+
" // BUG: Diagnostic contains: String result = null;",
218+
" AtomicReference<String> result = new AtomicReference<>();",
219+
" }",
220+
"}")
221+
.doTest();
222+
}
208223
}

0 commit comments

Comments
 (0)