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

Update comments referencing Refaster rule limitation #46

Merged
merged 11 commits into from
Dec 27, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ private ImmutableListMultimapRules() {}
* Prefer {@link ImmutableListMultimap#builder()} over the associated constructor on constructions
* that produce a less-specific type.
*/
// XXX: This drops generic type information, sometimes leading to non-compilable code. See
// https://github.com/google/error-prone/pull/2706.
static final class ImmutableListMultimapBuilder<K, V> {
@BeforeTemplate
ImmutableMultimap.Builder<K, V> before() {
Expand All @@ -48,7 +46,7 @@ ImmutableMultimap.Builder<K, V> before() {

@AfterTemplate
ImmutableListMultimap.Builder<K, V> after() {
return ImmutableListMultimap.builder();
return ImmutableListMultimap.<K, V>builder();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ final class ImmutableListRules {
private ImmutableListRules() {}

/** Prefer {@link ImmutableList#builder()} over the associated constructor. */
// XXX: This drops generic type information, sometimes leading to non-compilable code. See
// https://github.com/google/error-prone/pull/2706.
static final class ImmutableListBuilder<T> {
@BeforeTemplate
ImmutableList.Builder<T> before() {
Expand All @@ -38,7 +36,7 @@ ImmutableList.Builder<T> before() {

@AfterTemplate
ImmutableList.Builder<T> after() {
return ImmutableList.builder();
return ImmutableList.<T>builder();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ final class ImmutableMapRules {
private ImmutableMapRules() {}

/** Prefer {@link ImmutableMap#builder()} over the associated constructor. */
// XXX: This drops generic type information, sometimes leading to non-compilable code. See
// https://github.com/google/error-prone/pull/2706.
static final class ImmutableMapBuilder<K, V> {
@BeforeTemplate
ImmutableMap.Builder<K, V> before() {
Expand All @@ -40,7 +38,7 @@ ImmutableMap.Builder<K, V> before() {

@AfterTemplate
ImmutableMap.Builder<K, V> after() {
return ImmutableMap.builder();
return ImmutableMap.<K, V>builder();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ final class ImmutableMultisetRules {
private ImmutableMultisetRules() {}

/** Prefer {@link ImmutableMultiset#builder()} over the associated constructor. */
// XXX: This drops generic type information, sometimes leading to non-compilable code. See
// https://github.com/google/error-prone/pull/2706.
static final class ImmutableMultisetBuilder<T> {
@BeforeTemplate
ImmutableMultiset.Builder<T> before() {
Expand All @@ -31,7 +29,7 @@ ImmutableMultiset.Builder<T> before() {

@AfterTemplate
ImmutableMultiset.Builder<T> after() {
return ImmutableMultiset.builder();
return ImmutableMultiset.<T>builder();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ final class ImmutableSetMultimapRules {
private ImmutableSetMultimapRules() {}

/** Prefer {@link ImmutableSetMultimap#builder()} over the associated constructor. */
// XXX: This drops generic type information, sometimes leading to non-compilable code. See
// https://github.com/google/error-prone/pull/2706.
static final class ImmutableSetMultimapBuilder<K, V> {
@BeforeTemplate
ImmutableSetMultimap.Builder<K, V> before() {
Expand All @@ -39,7 +37,7 @@ ImmutableSetMultimap.Builder<K, V> before() {

@AfterTemplate
ImmutableSetMultimap.Builder<K, V> after() {
return ImmutableSetMultimap.builder();
return ImmutableSetMultimap.<K, V>builder();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ final class ImmutableSetRules {
private ImmutableSetRules() {}

/** Prefer {@link ImmutableSet#builder()} over the associated constructor. */
// XXX: This drops generic type information, sometimes leading to non-compilable code. See
// https://github.com/google/error-prone/pull/2706.
static final class ImmutableSetBuilder<T> {
@BeforeTemplate
ImmutableSet.Builder<T> before() {
Expand All @@ -39,7 +37,7 @@ ImmutableSet.Builder<T> before() {

@AfterTemplate
ImmutableSet.Builder<T> after() {
return ImmutableSet.builder();
return ImmutableSet.<T>builder();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ ImmutableSortedMap.Builder<K, V> after(Comparator<K> cmp) {
* Prefer {@link ImmutableSortedMap#naturalOrder()} over the alternative that requires explicitly
* providing the {@link Comparator}.
*/
// XXX: This drops generic type information, sometimes leading to non-compilable code. See
// https://github.com/google/error-prone/pull/2706.
static final class ImmutableSortedMapNaturalOrderBuilder<K extends Comparable<? super K>, V> {
@BeforeTemplate
ImmutableSortedMap.Builder<K, V> before() {
Expand All @@ -47,16 +45,14 @@ ImmutableSortedMap.Builder<K, V> before() {

@AfterTemplate
ImmutableSortedMap.Builder<K, V> after() {
return ImmutableSortedMap.naturalOrder();
return ImmutableSortedMap.<K, V>naturalOrder();
}
}

/**
* Prefer {@link ImmutableSortedMap#reverseOrder()} over the alternative that requires explicitly
* providing the {@link Comparator}.
*/
// XXX: This drops generic type information, sometimes leading to non-compilable code. See
// https://github.com/google/error-prone/pull/2706.
static final class ImmutableSortedMapReverseOrderBuilder<K extends Comparable<? super K>, V> {
@BeforeTemplate
ImmutableSortedMap.Builder<K, V> before() {
Expand All @@ -65,7 +61,7 @@ ImmutableSortedMap.Builder<K, V> before() {

@AfterTemplate
ImmutableSortedMap.Builder<K, V> after() {
return ImmutableSortedMap.reverseOrder();
return ImmutableSortedMap.<K, V>reverseOrder();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
Arrays.class, Collections.class, Comparator.class, Streams.class, naturalOrder());
}

ImmutableList.Builder<String> testImmutableListBuilder() {
return new ImmutableList.Builder<>();
ImmutableSet<ImmutableList.Builder<String>> testImmutableListBuilder() {
return ImmutableSet.of(new ImmutableList.Builder<>(), new ImmutableList.Builder<String>());
}

ImmutableSet<ImmutableList<Integer>> testIterableToImmutableList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
Arrays.class, Collections.class, Comparator.class, Streams.class, naturalOrder());
}

ImmutableList.Builder<String> testImmutableListBuilder() {
return ImmutableList.builder();
ImmutableSet<ImmutableList.Builder<String>> testImmutableListBuilder() {
return ImmutableSet.of(ImmutableList.builder(), ImmutableList<String>builder());
}

ImmutableSet<ImmutableList<Integer>> testIterableToImmutableList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Arrays.class, Collections.class, Streams.class, not(null));
}

ImmutableSet.Builder<String> testImmutableSetBuilder() {
return new ImmutableSet.Builder<>();
ImmutableSet<ImmutableSet.Builder<String>> testImmutableSetBuilder() {
return ImmutableSet.of(new ImmutableSet.Builder<>(), new ImmutableSet.Builder<Integer>());
}

ImmutableSet<ImmutableSet<Integer>> testIterableToImmutableSet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Arrays.class, Collections.class, Streams.class, not(null));
}

ImmutableSet.Builder<String> testImmutableSetBuilder() {
return ImmutableSet.builder();
ImmutableSet<ImmutableSet.Builder<String>> testImmutableSetBuilder() {
return ImmutableSet.of(ImmutableSet.builder(), ImmutableSet.<Integer>builder());
}

ImmutableSet<ImmutableSet<Integer>> testIterableToImmutableSet() {
Expand Down
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) {
return false;
}

return !((ParameterizedTypeTree) classTree.getIdentifier()).getTypeArguments().isEmpty();
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();",
" }",
"",
" 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();",
" }",
"",
" <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());",
" }",
"",
" 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());
}
}
}