Skip to content

Commit

Permalink
Fire InlineTrivialConstant on EMPTY and EMPTY_STR too.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 539700077
  • Loading branch information
kluever authored and Error Prone Team committed Jun 12, 2023
1 parent bda0edc commit 7f36856
Showing 1 changed file with 7 additions and 3 deletions.
Expand Up @@ -19,16 +19,17 @@
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;
import com.google.errorprone.VisitorState;
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;
Expand Down Expand Up @@ -107,6 +108,9 @@ private void handle(Tree tree) {
return NO_MATCH;
}

private static final ImmutableSet<String> EMPTY_STRING_VARIABLE_NAMES =
ImmutableSet.of("EMPTY", "EMPTY_STR", "EMPTY_STRING");

private static Optional<String> isTrivialConstant(
VariableTree tree, VarSymbol sym, VisitorState state) {
if (!(sym.getKind().equals(ElementKind.FIELD)
Expand All @@ -115,10 +119,10 @@ private static Optional<String> 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();
Expand Down

0 comments on commit 7f36856

Please sign in to comment.