From 7f368567c684fd9a18403541118ddaac45c4be6d Mon Sep 17 00:00:00 2001 From: Kurt Alfred Kluever Date: Mon, 12 Jun 2023 10:47:23 -0700 Subject: [PATCH] Fire `InlineTrivialConstant` on `EMPTY` and `EMPTY_STR` too. PiperOrigin-RevId: 539700077 --- .../errorprone/bugpatterns/InlineTrivialConstant.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/InlineTrivialConstant.java b/core/src/main/java/com/google/errorprone/bugpatterns/InlineTrivialConstant.java index 26dedbc399a..1341efc7b12 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/InlineTrivialConstant.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/InlineTrivialConstant.java @@ -19,8 +19,10 @@ import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; import static com.google.errorprone.matchers.Description.NO_MATCH; import static com.google.errorprone.util.ASTHelpers.getSymbol; +import static com.google.errorprone.util.ASTHelpers.isSameType; import com.google.auto.value.AutoValue; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.ListMultimap; import com.google.common.collect.MultimapBuilder; import com.google.errorprone.BugPattern; @@ -28,7 +30,6 @@ import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher; import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.matchers.Description; -import com.google.errorprone.util.ASTHelpers; import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.IdentifierTree; @@ -107,6 +108,9 @@ private void handle(Tree tree) { return NO_MATCH; } + private static final ImmutableSet EMPTY_STRING_VARIABLE_NAMES = + ImmutableSet.of("EMPTY", "EMPTY_STR", "EMPTY_STRING"); + private static Optional isTrivialConstant( VariableTree tree, VarSymbol sym, VisitorState state) { if (!(sym.getKind().equals(ElementKind.FIELD) @@ -115,10 +119,10 @@ private static Optional isTrivialConstant( && sym.getModifiers().contains(Modifier.FINAL))) { return Optional.empty(); } - if (!tree.getName().contentEquals("EMPTY_STRING")) { + if (!EMPTY_STRING_VARIABLE_NAMES.contains(tree.getName().toString())) { return Optional.empty(); } - if (!ASTHelpers.isSameType(sym.asType(), state.getSymtab().stringType, state)) { + if (!isSameType(sym.asType(), state.getSymtab().stringType, state)) { return Optional.empty(); } ExpressionTree initializer = tree.getInitializer();