Creating custom check - New to Java

69 views
Skip to first unread message

N T

unread,
Mar 15, 2021, 2:58:28 PM3/15/21
to error-prone-discuss
Hi there!

I am trying to follow the guidelines under “wiki>writing a check”  to re-create a plugin that detects “return null” statements but it doesn’t seem to be detecting it (Other in-built error-prone checks work). Is there something missing here ? 

Thank you.

N T

unread,
Mar 15, 2021, 3:03:51 PM3/15/21
to error-prone-discuss
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. 
Reply all
Reply to author
Forward
0 new messages