diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/NullRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/NullRules.java index 36a1ede0741..2d7a6b263b3 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/NullRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/NullRules.java @@ -21,16 +21,14 @@ final class NullRules { private NullRules() {} - /** Prefer the {@code ==} operator over {@link Objects#isNull(Object)}. */ + /** + * Prefer the {@code ==} operator (with {@code null} as the second operand) over {@link + * Objects#isNull(Object)}. + */ static final class IsNull { @BeforeTemplate boolean before(@Nullable Object object) { - return null == object; - } - - @BeforeTemplate - boolean before2(@Nullable Object object) { - return Objects.isNull(object); + return Refaster.anyOf(null == object, Objects.isNull(object)); } @AfterTemplate @@ -39,16 +37,14 @@ boolean after(@Nullable Object object) { } } - /** Prefer the {@code !=} operator over {@link Objects#nonNull(Object)}. */ + /** + * Prefer the {@code !=} operator (with {@code null} as the second operand) over {@link + * Objects#nonNull(Object)}. + */ static final class IsNotNull { @BeforeTemplate boolean before(@Nullable Object object) { - return null != object; - } - - @BeforeTemplate - boolean before2(@Nullable Object object) { - return Objects.nonNull(object); + return Refaster.anyOf(null != object, Objects.nonNull(object)); } @AfterTemplate diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PreconditionsRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PreconditionsRules.java index c6ba31ced56..351a7670068 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PreconditionsRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PreconditionsRules.java @@ -74,8 +74,22 @@ void after(int index, int size, String message) { } } - /** Prefer {@link Objects#requireNonNull(Object)} over more verbose alternatives. */ + /** Prefer {@link Objects#requireNonNull(Object)} over non-JDK alternatives. */ static final class RequireNonNull { + @BeforeTemplate + T before(T object) { + return checkNotNull(object); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + T after(T object) { + return requireNonNull(object); + } + } + + /** Prefer {@link Objects#requireNonNull(Object)} over more verbose alternatives. */ + static final class RequireNonNullStatement { @BeforeTemplate void before(T object) { if (object == null) { @@ -83,11 +97,6 @@ void before(T object) { } } - @BeforeTemplate - void before2(T object) { - checkNotNull(object); - } - @BeforeTemplate void before3(T object) { checkArgument(object != null); @@ -100,8 +109,22 @@ void after(T object) { } } - /** Prefer {@link Objects#requireNonNull(Object, String)} over more verbose alternatives. */ + /** Prefer {@link Objects#requireNonNull(Object, String)} over non-JDK alternatives. */ static final class RequireNonNullWithMessage { + @BeforeTemplate + T before2(T object, String message) { + return checkNotNull(object, message); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + T after(T object, String message) { + return requireNonNull(object, message); + } + } + + /** Prefer {@link Objects#requireNonNull(Object, String)} over more verbose alternatives. */ + static final class RequireNonNullWithMessageStatement { @BeforeTemplate void before(T object, String message) { if (object == null) { @@ -109,11 +132,6 @@ void before(T object, String message) { } } - @BeforeTemplate - void before2(T object, String message) { - checkNotNull(object, message); - } - @BeforeTemplate void before3(T object, String message) { checkArgument(object != null, message); diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestInput.java index aac35831213..bf465617ad4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestInput.java @@ -8,6 +8,7 @@ final class PreconditionsRulesTest implements RefasterRuleCollectionTestCase { @Override + @SuppressWarnings("RequireNonNull") public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(checkNotNull(null)); } @@ -30,20 +31,26 @@ void testCheckElementIndexWithMessage() { } } - void testRequireNonNull() { + String testRequireNonNull() { + return checkNotNull("foo"); + } + + void testRequireNonNullStatement() { if ("foo" == null) { throw new NullPointerException(); } - checkNotNull("bar"); - checkArgument("baz" != null); + checkArgument("bar" != null); + } + + String testRequireNonNullWithMessage() { + return checkNotNull("foo", "The string is null"); } - void testRequireNonNullWithMessage() { + void testRequireNonNullWithMessageStatement() { if ("foo" == null) { throw new NullPointerException("The string is null"); } - checkNotNull("bar", "The string is null"); - checkArgument("baz" != null, "The string is null"); + checkArgument("bar" != null, "The string is null"); } void testCheckPositionIndex() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestOutput.java index bdfddf62ba8..4cb8a674933 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PreconditionsRulesTestOutput.java @@ -12,6 +12,7 @@ final class PreconditionsRulesTest implements RefasterRuleCollectionTestCase { @Override + @SuppressWarnings("RequireNonNull") public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(checkNotNull(null)); } @@ -28,16 +29,22 @@ void testCheckElementIndexWithMessage() { checkElementIndex(1, 2, "My index"); } - void testRequireNonNull() { + String testRequireNonNull() { + return requireNonNull("foo"); + } + + void testRequireNonNullStatement() { requireNonNull("foo"); requireNonNull("bar"); - requireNonNull("baz"); } - void testRequireNonNullWithMessage() { + String testRequireNonNullWithMessage() { + return requireNonNull("foo", "The string is null"); + } + + void testRequireNonNullWithMessageStatement() { requireNonNull("foo", "The string is null"); requireNonNull("bar", "The string is null"); - requireNonNull("baz", "The string is null"); } void testCheckPositionIndex() {