ExplicitArgumentEnumeration
SUGGESTION
Performance
Simplification
Summary
Iterable creation can be avoided by using a varargs alternative method
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("ExplicitArgumentEnumeration")to the enclosing element.Disable this pattern completely by adding
-Xep:ExplicitArgumentEnumeration:OFFas compiler argument. Learn more.
Samples
Replacement
Shows the difference in example code before and after the bug pattern is applied.
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.bugpatterns.BugChecker;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
class A {
void m() {
- Flux.fromIterable(ImmutableList.of());
+ Flux.just();
- ImmutableList.builder().addAll(ImmutableList.of()).build();
+ ImmutableList.builder().add().build();
- assertThat(ImmutableList.of()).containsAnyElementsOf(ImmutableMultiset.of());
- assertThat(ImmutableList.of()).containsAll(ImmutableSet.of());
- assertThat(ImmutableList.of()).containsExactlyElementsOf(List.of());
- assertThat(ImmutableList.of()).containsExactlyInAnyOrderElementsOf(Set.of());
- assertThat(ImmutableList.of()).containsSequence(Arrays.asList());
- assertThat(ImmutableList.of()).containsSubsequence(ImmutableList.of(1));
- assertThat(ImmutableList.of()).doesNotContainAnyElementsOf(ImmutableMultiset.of(2));
- assertThat(ImmutableList.of()).doesNotContainSequence(ImmutableSet.of(3));
- assertThat(ImmutableList.of()).doesNotContainSubsequence(List.of(4));
- assertThat(ImmutableList.of()).hasSameElementsAs(Set.of(5));
- assertThat(ImmutableList.of()).isSubsetOf(Arrays.asList(6));
+ assertThat(ImmutableList.of()).containsAnyOf();
+ assertThat(ImmutableList.of()).contains();
+ assertThat(ImmutableList.of()).containsExactly();
+ assertThat(ImmutableList.of()).containsExactlyInAnyOrder();
+ assertThat(ImmutableList.of()).containsSequence();
+ assertThat(ImmutableList.of()).containsSubsequence(1);
+ assertThat(ImmutableList.of()).doesNotContain(2);
+ assertThat(ImmutableList.of()).doesNotContainSequence(3);
+ assertThat(ImmutableList.of()).doesNotContainSubsequence(4);
+ assertThat(ImmutableList.of()).containsOnly(5);
+ assertThat(ImmutableList.of()).isSubsetOf(6);
- Flux.empty()
- .as(StepVerifier::create)
- .expectNextSequence(ImmutableList.of(1, 2))
- .verifyComplete();
+ Flux.empty().as(StepVerifier::create).expectNext(1, 2).verifyComplete();
- BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass())
- .setArgs(ImmutableList.of("foo", "bar"));
- CompilationTestHelper.newInstance(BugChecker.class, getClass())
- .setArgs(ImmutableList.of("foo", "bar"));
+ BugCheckerRefactoringTestHelper.newInstance(BugChecker.class, getClass()).setArgs("foo", "bar");
+ CompilationTestHelper.newInstance(BugChecker.class, getClass()).setArgs("foo", "bar");
- ImmutableListMultimap.builder().putAll(1, ImmutableList.of(2));
- ImmutableSetMultimap.<String, Integer>builder().putAll("foo", ImmutableSet.of(2));
+ ImmutableListMultimap.builder().putAll(1, 2);
+ ImmutableSetMultimap.<String, Integer>builder().putAll("foo", 2);
}
}
Identification
Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.bugpatterns.BugChecker;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Tag;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
class A {
// BUG: Diagnostic contains:
private final int value = unaryMethod(ImmutableList.of(1, 2));
void m() {
ImmutableList<String> list = ImmutableList.of();
assertThat(ImmutableList.of()).containsAnyElementsOf(list);
ImmutableList.<ImmutableList<String>>builder().add(ImmutableList.of());
Counter.builder("foo").tags(ImmutableList.of(Tag.of("bar", "baz")));
// BUG: Diagnostic contains:
unaryMethod(ImmutableList.of(1, 2));
unaryMethod(1, 2);
unaryMethodWithLessVisibleOverload(ImmutableList.of(1, 2));
unaryMethodWithIncompatibleReturnType(ImmutableList.of(1, 2));
unaryMethodWithExtendsIncompatibleVarargsOverload(ImmutableList.of(1, 2));
unaryMethodWithSuperIncompatibleVarargsOverload(ImmutableList.of(1, 2));
// BUG: Diagnostic contains:
binaryMethod(0, ImmutableList.of(1, 2));
binaryMethod(0, 1, 2);
binaryMethodWithIncompatibleOverload(0, ImmutableList.of(1, 2));
binaryMethodWithUnaryOverload(new Integer[0], ImmutableList.of(2, 3));
// BUG: Diagnostic contains:
rawListMethod(ImmutableList.of(1, 2));
rawListMethodWithTypeRestrictedOverload(ImmutableList.of(1, 2));
// BUG: Diagnostic contains:
Flux.fromIterable(ImmutableList.of());
// BUG: Diagnostic contains:
Flux.fromIterable(ImmutableList.copyOf(new String[0]));
Flux.fromIterable(ImmutableList.copyOf(ImmutableList.of()));
ImmutableList.builder()
// BUG: Diagnostic contains:
.addAll(ImmutableList.of(1))
.build();
assertThat(ImmutableList.of(1))
// BUG: Diagnostic contains:
.containsAnyElementsOf(ImmutableList.of(1))
// BUG: Diagnostic contains:
.isSubsetOf(ImmutableList.of(1));
Flux.just(1, 2)
.as(StepVerifier::create)
// BUG: Diagnostic contains:
.expectNextSequence(ImmutableList.of(1, 2))
.verifyComplete();
CompilationTestHelper.newInstance(BugChecker.class, getClass())
// BUG: Diagnostic contains:
.setArgs(ImmutableList.of("foo"))
.withClasspath();
ImmutableListMultimap.builder()
// BUG: Diagnostic contains:
.putAll(1, ImmutableList.of(2));
ImmutableSetMultimap.<String, Integer>builder()
// BUG: Diagnostic contains:
.putAll("foo", ImmutableSet.of(1, 2));
}
private int unaryMethod(ImmutableList<Integer> args) {
return 0;
}
private int unaryMethod(Integer... args) {
return unaryMethod(ImmutableList.copyOf(args));
}
void unaryMethodWithLessVisibleOverload(ImmutableList<Integer> args) {}
private void unaryMethodWithLessVisibleOverload(Integer... args) {
unaryMethodWithLessVisibleOverload(ImmutableList.copyOf(args));
}
private Integer unaryMethodWithIncompatibleReturnType(ImmutableList<Integer> args) {
return 0;
}
private Object unaryMethodWithIncompatibleReturnType(Integer... args) {
return unaryMethodWithIncompatibleReturnType(ImmutableList.copyOf(args));
}
private int unaryMethodWithExtendsIncompatibleVarargsOverload(
ImmutableList<? extends Number> args) {
return 0;
}
private int unaryMethodWithExtendsIncompatibleVarargsOverload(Integer... args) {
return unaryMethodWithExtendsIncompatibleVarargsOverload(ImmutableList.copyOf(args));
}
private int unaryMethodWithSuperIncompatibleVarargsOverload(ImmutableList<? super Integer> args) {
return 0;
}
private int unaryMethodWithSuperIncompatibleVarargsOverload(Integer... args) {
return unaryMethodWithSuperIncompatibleVarargsOverload(ImmutableList.copyOf(args));
}
private int binaryMethod(Integer extraArg, ImmutableList<Integer> args) {
return 0;
}
private int binaryMethod(Object extraArg, Integer... args) {
return binaryMethod(0, ImmutableList.copyOf(args));
}
private int binaryMethodWithIncompatibleOverload(Object extraArg, ImmutableList<Integer> args) {
return 0;
}
private int binaryMethodWithIncompatibleOverload(Integer extraArg, Integer... args) {
return binaryMethodWithIncompatibleOverload(0, ImmutableList.copyOf(args));
}
private void binaryMethodWithUnaryOverload(Integer[] extraArg, ImmutableList<Integer> args) {}
private void binaryMethodWithUnaryOverload(Integer... args) {
binaryMethodWithUnaryOverload(args, ImmutableList.copyOf(args));
}
private void rawListMethod(ImmutableList args) {}
private void rawListMethod(Object... args) {
rawListMethod(ImmutableList.copyOf(args));
}
private void rawListMethodWithTypeRestrictedOverload(ImmutableList args) {}
private void rawListMethodWithTypeRestrictedOverload(String... args) {
rawListMethodWithTypeRestrictedOverload(ImmutableList.copyOf(args));
}
}