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

Introduce Check{Argument,State}WithMessageAndArguments Refaster rules #1135

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 @@ -9,8 +9,11 @@
import static java.util.Objects.requireNonNull;

import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.FormatMethod;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.Repeated;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Objects;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
Expand Down Expand Up @@ -52,6 +55,26 @@ void after(boolean condition, String message) {
}
}

/**
* Prefer {@link Preconditions#checkArgument(boolean, String, Object...)} over more verbose
* alternatives.
*/
// XXX: This check assumes that the format string only uses `%s` placeholders.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this isn't fully correct. One option is to create an BugChecker instead, but that's more work 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I went down the rabbit hole: #1139.

static final class CheckArgumentWithMessageAndArguments {
@BeforeTemplate
@FormatMethod
void before(boolean condition, String message, @Repeated Object... args) {
checkArgument(
condition, Refaster.anyOf(String.format(message, args), message.formatted(args)));
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(boolean condition, String message, @Repeated Object... args) {
checkArgument(condition, message, args);
}
}

/**
* Prefer {@link Preconditions#checkElementIndex(int, int, String)} over less descriptive or more
* verbose alternatives.
Expand Down Expand Up @@ -205,4 +228,23 @@ void after(boolean condition, String message) {
checkState(!condition, message);
}
}

/**
* Prefer {@link Preconditions#checkState(boolean, String, Object...)} over more verbose
* alternatives.
*/
// XXX: This check assumes that the format string only uses `%s` placeholders.
static final class CheckStateWithMessageAndArguments {
@BeforeTemplate
@FormatMethod
void before(boolean condition, String message, @Repeated Object... args) {
checkState(condition, Refaster.anyOf(String.format(message, args), message.formatted(args)));
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(boolean condition, String message, @Repeated Object... args) {
checkState(condition, message, args);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tech.picnic.errorprone.refasterrules;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.collect.ImmutableSet;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
Expand All @@ -24,6 +26,11 @@ void testCheckArgumentWithMessage() {
}
}

void testCheckArgumentWithMessageAndArguments() {
checkArgument("foo".isEmpty(), String.format("The %s is empty", 1));
checkArgument("bar".isEmpty(), "The %s is %s".formatted(2.0, false));
}

void testCheckElementIndexWithMessage() {
if (1 < 0 || 1 >= 2) {
throw new IndexOutOfBoundsException("My index");
Expand Down Expand Up @@ -73,4 +80,9 @@ void testCheckStateWithMessage() {
throw new IllegalStateException("The string is empty");
}
}

void testCheckStateWithMessageAndArguments() {
checkState("foo".isEmpty(), String.format("The %s is empty", 1));
checkState("bar".isEmpty(), "The %s is %s".formatted(2.0, false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ void testCheckArgumentWithMessage() {
checkArgument(!"foo".isEmpty(), "The string is empty");
}

void testCheckArgumentWithMessageAndArguments() {
checkArgument("foo".isEmpty(), "The %s is empty", 1);
checkArgument("bar".isEmpty(), "The %s is %s", 2.0, false);
}

void testCheckElementIndexWithMessage() {
checkElementIndex(1, 2, "My index");
}
Expand Down Expand Up @@ -60,4 +65,9 @@ void testCheckState() {
void testCheckStateWithMessage() {
checkState(!"foo".isEmpty(), "The string is empty");
}

void testCheckStateWithMessageAndArguments() {
checkState("foo".isEmpty(), "The %s is empty", 1);
checkState("bar".isEmpty(), "The %s is %s", 2.0, false);
}
}