Skip to content

Commit

Permalink
Suppress NonFinalStaticField warning for volatile fields.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 703641432
  • Loading branch information
java-team-github-bot authored and Error Prone Team committed Dec 6, 2024
1 parent 089aa9b commit 5d4c57d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 5d4c57d

Please sign in to comment.