Skip to content

Commit

Permalink
Don't additionally suggest var unused = ... or removing the stateme…
Browse files Browse the repository at this point in the history
…nt for an `assertThat(...)` call with a missing `isTrue()` call.

`isTrue()` is essentially guaranteed to be correct, so the additional suggestion is just noise and an invitation to accidentally apply the wrong fix.

PiperOrigin-RevId: 520035528
  • Loading branch information
cpovirk authored and Error Prone Team committed Mar 29, 2023
1 parent e432465 commit 9eed554
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ final ImmutableList<Fix> fixesAtCallSite(ExpressionTree invocationTree, VisitorS
}
}
}
boolean considerBlanketFixes = true;
if (resultType != null && resultType.getKind() == TypeKind.BOOLEAN) {
// Fix by calling either assertThat(...).isTrue() or verify(...).
if (state.errorProneOptions().isTestOnlyTarget()) {
Expand All @@ -328,6 +329,7 @@ && matchingMethods(
state.getTypes())
.anyMatch(m -> true)) {
fixes.put("Assert that the result is true", postfixWith(invocationTree, ".isTrue()"));
considerBlanketFixes = false;
}
if (identifierExpr != null
&& symbol != null
Expand All @@ -344,7 +346,8 @@ && matchingMethods(
* this a constructor call or build() call?"
*/
if (parent.getKind() == EXPRESSION_STATEMENT
&& !constantExpressions.constantExpression(invocationTree, state).isPresent()) {
&& !constantExpressions.constantExpression(invocationTree, state).isPresent()
&& considerBlanketFixes) {
ImmutableSet<String> identifiersInScope =
findAllIdents(state).stream().map(v -> v.name.toString()).collect(toImmutableSet());
concat(Stream.of("unused"), range(2, 10).mapToObj(i -> "unused" + i))
Expand All @@ -358,7 +361,7 @@ && matchingMethods(
"Suppress error by assigning to a variable",
prefixWith(parent, format("var %s = ", n))));
}
if (parent.getKind() == EXPRESSION_STATEMENT) {
if (parent.getKind() == EXPRESSION_STATEMENT && considerBlanketFixes) {
if (constantExpressions.constantExpression(invocationTree, state).isPresent()) {
fixes.put("Delete call", delete(parent));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.auto.value.processor.AutoBuilderProcessor;
import com.google.auto.value.processor.AutoValueProcessor;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.bugpatterns.CheckReturnValue;
Expand Down Expand Up @@ -273,6 +274,7 @@ public void truthMissingIsTrue() {
" assertThat(b).isTrue();",
" }",
"}")
.setFixChooser(Iterables::getOnlyElement)
.doTest();
}

Expand Down

0 comments on commit 9eed554

Please sign in to comment.