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

UnnecessaryCheckNotNull correctly qualifies Preconditions #1433

Closed
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -30,6 +30,7 @@
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
import com.google.errorprone.fixes.Fix;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
Expand Down Expand Up @@ -67,9 +68,10 @@
providesFix = ProvidesFix.REQUIRES_HUMAN_ATTENTION)
public class UnnecessaryCheckNotNull extends BugChecker implements MethodInvocationTreeMatcher {

private static final String PRECONDITIONS = "com.google.common.base.Preconditions";
private static final Matcher<MethodInvocationTree> CHECK_NOT_NULL_MATCHER =
Matchers.<MethodInvocationTree>anyOf(
staticMethod().onClass("com.google.common.base.Preconditions").named("checkNotNull"),
staticMethod().onClass(PRECONDITIONS).named("checkNotNull"),
staticMethod().onClass("com.google.common.base.Verify").named("verifyNotNull"),
staticMethod().onClass("java.util.Objects").named("requireNonNull"));

Expand Down Expand Up @@ -186,7 +188,7 @@ private static Fix createCheckArgumentOrStateCall(
if (methodInvocationTree.getMethodSelect().getKind() == Kind.IDENTIFIER) {
fix.addStaticImport("com.google.common.base.Preconditions." + replacementMethod);
} else {
replacement.append("Preconditions.");
replacement.append(SuggestedFixes.qualifyType(state, fix, PRECONDITIONS)).append('.');
}
replacement.append(replacementMethod).append('(');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.VisitorState;
import com.google.errorprone.matchers.CompilerBasedAbstractTest;
Expand Down Expand Up @@ -135,6 +136,32 @@ public void positive_newArray() {
.doTest();
}

@Test
public void positive_qualifiedImport() {
BugCheckerRefactoringTestHelper.newInstance(new UnnecessaryCheckNotNull(), getClass())
.addInputLines(
"Test.java",
"import com.google.common.base.Verify;",
"import java.util.Objects;",
"class Test {",
"void negative() {",
"Verify.verifyNotNull(getClass().getName().contains(\"value\"));",
"}",
"static final class Preconditions {}",
"}")
.addOutputLines(
"Test.java",
"import com.google.common.base.Verify;",
"import java.util.Objects;",
"class Test {",
"void negative() {",
"com.google.common.base.Preconditions.checkArgument(getClass().getName().contains(\"value\"));",
"}",
"static final class Preconditions {}",
"}")
.doTest();
}

@Test
public void negative() {
compilationHelper
Expand Down