Skip to content

Commit

Permalink
Cover checkArgument and reversed null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien Diederichs committed Mar 7, 2023
1 parent 5439540 commit 4b8eb2c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static java.util.Objects.requireNonNull;

import com.google.common.base.Preconditions;
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.UseImportPolicy;
Expand Down Expand Up @@ -78,7 +79,7 @@ void after(int index, int size, String message) {
static final class RequireNonNull<T> {
@BeforeTemplate
void before(T object) {
if (object == null) {
if (Refaster.anyOf(object == null, null == object)) {
throw new NullPointerException();
}
}
Expand All @@ -88,6 +89,11 @@ void before2(T object) {
checkNotNull(object);
}

@BeforeTemplate
void before3(T object) {
checkArgument(Refaster.anyOf(object != null, null != object));
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(T object) {
Expand All @@ -99,7 +105,7 @@ void after(T object) {
static final class RequireNonNullWithMessage<T> {
@BeforeTemplate
void before(T object, String message) {
if (object == null) {
if (Refaster.anyOf(object == null, null == object)) {
throw new NullPointerException(message);
}
}
Expand All @@ -109,6 +115,11 @@ void before2(T object, String message) {
checkNotNull(object, message);
}

@BeforeTemplate
void before3(T object, String message) {
checkArgument(Refaster.anyOf(object != null, null != object), message);
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(T object, String message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

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

import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -33,14 +34,24 @@ void testRequireNonNull() {
if ("foo" == null) {
throw new NullPointerException();
}
if (null == "foo") {
throw new NullPointerException();
}
checkNotNull("foo");
checkArgument("foo" != null);
checkArgument(null != "foo");
}

void testRequireNonNullWithMessage() {
if ("foo" == null) {
throw new NullPointerException("The string is null");
}
if (null == "foo") {
throw new NullPointerException("The string is null");
}
checkNotNull("foo", "The string is null");
checkArgument("foo" != null, "The string is null");
checkArgument(null != "foo", "The string is null");
}

void testCheckPositionIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ void testCheckElementIndexWithMessage() {
void testRequireNonNull() {
requireNonNull("foo");
requireNonNull("foo");
requireNonNull("foo");
requireNonNull("foo");
requireNonNull("foo");
}

void testRequireNonNullWithMessage() {
requireNonNull("foo", "The string is null");
requireNonNull("foo", "The string is null");
requireNonNull("foo", "The string is null");
requireNonNull("foo", "The string is null");
requireNonNull("foo", "The string is null");
}

void testCheckPositionIndex() {
Expand Down

0 comments on commit 4b8eb2c

Please sign in to comment.