diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/NonFinalStaticField.java b/core/src/main/java/com/google/errorprone/bugpatterns/NonFinalStaticField.java index f9a1d7cf660..43e6dfeba0e 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/NonFinalStaticField.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/NonFinalStaticField.java @@ -20,7 +20,6 @@ import static com.google.errorprone.fixes.SuggestedFix.emptyFix; import static com.google.errorprone.fixes.SuggestedFix.merge; import static com.google.errorprone.fixes.SuggestedFixes.addModifiers; -import static com.google.errorprone.fixes.SuggestedFixes.removeModifiers; import static com.google.errorprone.matchers.Description.NO_MATCH; import static com.google.errorprone.util.ASTHelpers.canBeRemoved; import static com.google.errorprone.util.ASTHelpers.getSymbol; @@ -68,6 +67,9 @@ public Description matchVariable(VariableTree tree, VisitorState state) { if (!isStatic(symbol)) { return NO_MATCH; } + if (symbol.getModifiers().contains(Modifier.VOLATILE)) { + return NO_MATCH; + } if (isConsideredFinal(symbol)) { return NO_MATCH; } @@ -87,8 +89,6 @@ public Description matchVariable(VariableTree tree, VisitorState state) { merge( addModifiers(tree, tree.getModifiers(), state, ImmutableSet.of(FINAL)) .orElse(emptyFix()), - removeModifiers(tree.getModifiers(), state, ImmutableSet.of(Modifier.VOLATILE)) - .orElse(emptyFix()), addDefaultInitializerIfNecessary(tree, state))); } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/NonFinalStaticFieldTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/NonFinalStaticFieldTest.java index 8892688010a..4d234f06b1b 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/NonFinalStaticFieldTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/NonFinalStaticFieldTest.java @@ -233,22 +233,15 @@ public class Test { } @Test - public void volatileRemoved() { - refactoringTestHelper - .addInputLines( + public void volatile_noFinding() { + compilationTestHelper + .addSourceLines( "Test.java", """ public class Test { private static volatile String FOO = ""; } """) - .addOutputLines( - "Test.java", - """ - public class Test { - private static final String FOO = ""; - } - """) .doTest(); }