Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SuppressWarningsDeprecated: rewrite using simpler library method calls, and remove AbstractSuppressWarningsMatcher base class. #2635

Merged
merged 1 commit into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

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() {}
}