Skip to content

Commit

Permalink
Introduce JUnitSingleArguments bug checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Venorcis committed Oct 6, 2023
1 parent 5596b58 commit 29bdf78
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tech.picnic.errorprone.bugpatterns;

import static com.google.errorprone.BugPattern.LinkType.CUSTOM;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL;

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.MethodInvocationTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import tech.picnic.errorprone.bugpatterns.util.SourceCode;

/**
* A {@link BugChecker} that flags uses of {@link
* org.junit.jupiter.params.provider.Arguments#arguments(Object...)} with a single (or no) argument;
* in such cases the use of {@link org.junit.jupiter.params.provider.Arguments} is not required as a
* {@link java.util.stream.Stream} of objects can be directly provided to a {@link
* org.junit.jupiter.params.provider.MethodSource}.
*/
@AutoService(BugChecker.class)
@BugPattern(
summary = "JUnit arguments wrapping a single object are redundant",
link = BUG_PATTERNS_BASE_URL + "JUnitSingleArguments",
linkType = CUSTOM,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class JUnitSingleArguments extends BugChecker implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<ExpressionTree> ARGUMENTS_ARGUMENTS =
staticMethod().onClass("org.junit.jupiter.params.provider.Arguments").named("arguments");

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

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (ARGUMENTS_ARGUMENTS.matches(tree, state) && tree.getArguments().size() <= 1) {

Check warning on line 44 in error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitSingleArguments.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 44 without causing a test to fail

removed conditional - replaced equality check with true (covered by 1 tests RemoveConditionalMutator_EQUAL_IF)
return describeMatch(tree, SourceCode.unwrapMethodInvocation(tree, state));
}

return Description.NO_MATCH;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tech.picnic.errorprone.bugpatterns;

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

final class JUnitSingleArgumentsTest {
@Test
void identification() {
CompilationTestHelper.newInstance(JUnitSingleArguments.class, getClass())
.addSourceLines(
"A.java",
"import static org.junit.jupiter.params.provider.Arguments.arguments;",
"import java.util.stream.Stream;",
"",
"class A {",
" void m() {",
" // BUG: Diagnostic contains:",
" arguments();",
" // BUG: Diagnostic contains:",
" arguments(1);",
" arguments(1,2);",
" }",
"}")
.doTest();
}
}

0 comments on commit 29bdf78

Please sign in to comment.