diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryAsync.java b/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryAsync.java index 8a978fd1f06..29b9c176779 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryAsync.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryAsync.java @@ -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() { @@ -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": @@ -228,13 +229,14 @@ 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": @@ -242,8 +244,8 @@ private static String getDefaultInitializer(VarSymbol symbol) { 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(); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryAsyncTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryAsyncTest.java index 8d46355b9a7..a817e8504c8 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryAsyncTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryAsyncTest.java @@ -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 result = new AtomicReference<>();", + " }", + "}") + .doTest(); + } }