Code
package org.example;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ReturnTree;
import com.sun.source.tree.Tree;
import static com.google.errorprone.BugPattern.SeverityLevel.
ERROR;
import static com.google.errorprone.matchers.Matchers.
contains;
import static com.sun.source.tree.Tree.Kind.
NULL_LITERAL;
/**
* Bug checker to detect usage of useless increment or decrement in return.
*/
@AutoService(BugChecker.class)
@BugPattern(
name = "NullReturn",
summary = "RETURN NULL STATEMENTS",
severity =
ERROR)
public class NullReturn extends BugChecker implements MethodTreeMatcher {
private static final Matcher<Tree>
RETURN_NULL = new ReturnUselessIncrementMatcher();
private static final Matcher<Tree>
CONTAINS_RETURN_=
contains(
RETURN_NULL);
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
if (!
CONTAINS_RETURN.matches(tree, state)) {
return Description.
NO_MATCH;
}
return describeMatch(tree);
}
private static class ReturnUselessIncrementMatcher implements Matcher<Tree> {
@Override
public boolean matches(Tree tree, VisitorState state) {
if (tree instanceof ReturnTree) {
return ((ReturnTree) tree).getExpression().getKind() ==
NULL_LITERAL;
}
return false;
}
}
}
The code compiles but the check doesn't seem to be working.