Skip to content

Commit

Permalink
Fix a crash in UnnecessaryAsync
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 567041651
  • Loading branch information
cushon authored and Error Prone Team committed Sep 20, 2023
1 parent d2ee28e commit 1bec842
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private SuggestedFix attemptFix(VariableTree tree, VisitorState state) {
getPrimitiveType(symbol.type, state.getTypes()),
symbol.getSimpleName(),
constructor.getArguments().isEmpty()
? getDefaultInitializer(symbol)
? getDefaultInitializer(symbol, state.getTypes())
: state.getSourceForNode(constructor.getArguments().get(0))));

new TreePathScanner<Void, Void>() {
Expand Down Expand Up @@ -217,7 +217,8 @@ private boolean isVariableDeclarationItself(Tree parentTree) {
}

private static String getPrimitiveType(Type type, Types types) {
switch (types.erasure(type).toString()) {
String name = types.erasure(type).toString();
switch (name) {
case "java.util.concurrent.atomic.AtomicBoolean":
return "boolean";
case "java.util.concurrent.atomic.AtomicReference":
Expand All @@ -228,22 +229,23 @@ private static String getPrimitiveType(Type type, Types types) {
return "int";
case "java.util.concurrent.atomic.AtomicLong":
return "long";
default: // fall out
default:
throw new AssertionError(name);
}
throw new AssertionError();
}

private static String getDefaultInitializer(VarSymbol symbol) {
switch (symbol.type.toString()) {
private static String getDefaultInitializer(VarSymbol symbol, Types types) {
String name = types.erasure(symbol.type).toString();
switch (name) {
case "java.util.concurrent.atomic.AtomicBoolean":
return "false";
case "java.util.concurrent.atomic.AtomicReference":
return "null";
case "java.util.concurrent.atomic.AtomicInteger":
case "java.util.concurrent.atomic.AtomicLong":
return "0";
default: // fall out
default:
throw new AssertionError(name);
}
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,19 @@ public void negative_capturedByLambda() {
"}")
.doTest();
}

@Test
public void atomicReference_unfixable() {
helper
.addSourceLines(
"Test.java",
"import java.util.concurrent.atomic.AtomicReference;",
"class Test {",
" void f() {",
" // BUG: Diagnostic contains: String result = null;",
" AtomicReference<String> result = new AtomicReference<>();",
" }",
"}")
.doTest();
}
}

0 comments on commit 1bec842

Please sign in to comment.