Skip to content

Commit

Permalink
SuppressWarningsDeprecated: rewrite using simpler library method call…
Browse files Browse the repository at this point in the history
…s, and remove AbstractSuppressWarningsMatcher base class.

PiperOrigin-RevId: 404211313
  • Loading branch information
awturner authored and Error Prone Team committed Oct 19, 2021
1 parent 34d98e8 commit 472b2dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 95 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
import static com.google.errorprone.matchers.Matchers.hasArgumentWithValue;
import static com.google.errorprone.matchers.Matchers.isType;
import static com.google.errorprone.matchers.Matchers.stringLiteral;
import static java.util.stream.Collectors.toList;

import com.google.auto.common.AnnotationMirrors;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.MoreAnnotations;
import com.sun.source.tree.AnnotationTree;
import java.util.List;
import javax.lang.model.element.AnnotationMirror;

/**
* Find uses of SuppressWarnings with "deprecated".
Expand All @@ -38,28 +45,28 @@
name = "SuppressWarningsDeprecated",
summary = "Suppressing \"deprecated\" is probably a typo for \"deprecation\"",
severity = ERROR)
public class SuppressWarningsDeprecated extends AbstractSuppressWarningsMatcher {
public class SuppressWarningsDeprecated extends BugChecker implements AnnotationTreeMatcher {

@SuppressWarnings("varargs")
private static final Matcher<AnnotationTree> matcher =
allOf(
isType("java.lang.SuppressWarnings"),
hasArgumentWithValue("value", stringLiteral("deprecated")));

@Override
public final Description matchAnnotation(AnnotationTree annotationTree, VisitorState state) {
if (matcher.matches(annotationTree, state)) {
return describeMatch(annotationTree, getSuggestedFix(annotationTree));
if (!matcher.matches(annotationTree, state)) {
return Description.NO_MATCH;
}
return Description.NO_MATCH;
}

@Override
protected void processSuppressWarningsValues(List<String> values) {
for (int i = 0; i < values.size(); i++) {
if (values.get(i).equals("deprecated")) {
values.set(i, "deprecation");
}
}
AnnotationMirror mirror = ASTHelpers.getAnnotationMirror(annotationTree);
List<String> values =
MoreAnnotations.asStrings(AnnotationMirrors.getAnnotationValue(mirror, "value"))
.map(v -> v.equals("deprecated") ? "deprecation" : v)
.map(state::getConstantExpression)
.collect(toList());

return describeMatch(
annotationTree,
SuggestedFixes.updateAnnotationArgumentValues(annotationTree, "value", values).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public static void positiveCase7() {
class Foo {};
}

// BUG: Diagnostic contains: @SuppressWarnings("deprecation")
// BUG: Diagnostic contains: @SuppressWarnings(value = "deprecation")
@SuppressWarnings(value = {"deprecated"})
public static void positiveCase8() {}

// BUG: Diagnostic contains: @SuppressWarnings("deprecation")
// BUG: Diagnostic contains: @SuppressWarnings(value = "deprecation")
@SuppressWarnings(value = "deprecated")
public static void positiveCase9() {}
}

0 comments on commit 472b2dc

Please sign in to comment.