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

Introduce HasTypeArguments matcher #644

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tech.picnic.errorprone.refaster.matchers;

import com.google.errorprone.VisitorState;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.Tree;

/** A matcher of expressions with type arguments. */
public final class HasTypeArguments implements Matcher<ExpressionTree> {
private static final long serialVersionUID = 1L;

/** Instantiates a new {@link HasTypeArguments} instance. */
public HasTypeArguments() {}

@Override
public boolean matches(ExpressionTree tree, VisitorState state) {
switch (tree.getKind()) {
case METHOD_INVOCATION:
return !((MethodInvocationTree) tree).getTypeArguments().isEmpty();
case NEW_CLASS:
NewClassTree classTree = (NewClassTree) tree;
if (!classTree.getTypeArguments().isEmpty()) {
return true;
}

if (classTree.getIdentifier().getKind() != Tree.Kind.PARAMETERIZED_TYPE) {
oxkitsune marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

return !((ParameterizedTypeTree) classTree.getIdentifier()).getTypeArguments().isEmpty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can extract classTree.getIdentifier() as we use it twice.

default:
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tech.picnic.errorprone.refaster.matchers;

import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;

import com.google.errorprone.BugPattern;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.bugpatterns.BugChecker;
import org.junit.jupiter.api.Test;

final class HasTypeArgumentsTest {
@Test
void matches() {
CompilationTestHelper.newInstance(MatcherTestChecker.class, getClass())
.addSourceLines(
"A.java",
"import com.google.common.collect.ImmutableSet;",
"import java.util.ArrayList;",
"import java.util.List;",
"",
"class A {",
" Object negative1() {",
" return alwaysNull();",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simply use toString() here :)

" }",
"",
" Object negative2() {",
" return new Object();",
" }",
"",
" List<Integer> negative3() {",
" return new ArrayList<>();",
" }",
"",
" Foo negative4() {",
" return new Foo(\"foo\");",
" }",
"",
" <E> ImmutableSet<E> positive1() {",
" // BUG: Diagnostic contains:",
" return ImmutableSet.<E>builder().build();",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably simplify by:

"  List<Integer> positive1() {",
"    return new ArrayList<Integer>();",
"  }",
"",

(Will apply this tomorrow when I get back to this.

" }",
"",
" <E> ImmutableSet<E> positive2() {",
" // BUG: Diagnostic contains:",
" return new ImmutableSet.Builder<E>().build();",
" }",
"",
" Foo positive3() {",
" // BUG: Diagnostic contains:",
" return new <List<Object>>Foo(List.of());",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's is probably a better example of this

" }",
"",
" private static <T> T alwaysNull() {",
" return null;",
" }",
"",
" public static class Foo {",
" private final Object value;",
"",
" public Foo(Object value) {",
" this.value = value;",
" }",
"",
" public <E extends List<Object>> Foo(E value) {",
" this.value = value;",
" }",
" }",
"}")
.doTest();
}

/** A {@link BugChecker} that simply delegates to {@link HasTypeArguments}. */
@BugPattern(summary = "Flags expressions matched by `HasTypeArguments`", severity = ERROR)
public static final class MatcherTestChecker extends AbstractMatcherTestChecker {
private static final long serialVersionUID = 1L;

// XXX: This is a false positive reported by Checkstyle. See
// https://github.com/checkstyle/checkstyle/issues2/10161#issuecomment-1242732120.
@SuppressWarnings("RedundantModifier")
public MatcherTestChecker() {
super(new HasTypeArguments());
}
}
}