Skip to content

Commit

Permalink
Extend {Is,Non}NullFunction Refaster rules (#1494)
Browse files Browse the repository at this point in the history
While there, simplify the associated tests.
  • Loading branch information
werli authored Dec 31, 2024
1 parent 667f83f commit d11ac09
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Objects.requireNonNullElse;
import static java.util.Objects.requireNonNullElseGet;
import static java.util.function.Predicate.not;

import com.google.common.base.MoreObjects;
import com.google.errorprone.refaster.Refaster;
Expand Down Expand Up @@ -94,11 +95,14 @@ T after(T object, Supplier<S> supplier) {
}
}

/** Prefer {@link Objects#isNull(Object)} over the equivalent lambda function. */
/**
* Prefer {@link Objects#isNull(Object)} over the equivalent lambda function or more contrived
* alternatives.
*/
static final class IsNullFunction<T> {
@BeforeTemplate
Predicate<T> before() {
return o -> o == null;
return Refaster.anyOf(o -> o == null, not(Objects::nonNull));
}

@AfterTemplate
Expand All @@ -107,11 +111,14 @@ Predicate<T> after() {
}
}

/** Prefer {@link Objects#nonNull(Object)} over the equivalent lambda function. */
/**
* Prefer {@link Objects#nonNull(Object)} over the equivalent lambda function or more contrived
* alternatives.
*/
static final class NonNullFunction<T> {
@BeforeTemplate
Predicate<T> before() {
return o -> o != null;
return Refaster.anyOf(o -> o != null, not(Objects::isNull));
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.function.Predicate.not;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.function.Predicate;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class NullRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(MoreObjects.class, Optional.class);
return ImmutableSet.of(MoreObjects.class, Optional.class, not(null));
}

ImmutableSet<Boolean> testIsNull() {
Expand All @@ -30,11 +32,11 @@ String testRequireNonNullElseGet() {
return Optional.ofNullable("foo").orElseGet(() -> "bar");
}

long testIsNullFunction() {
return Stream.of("foo").filter(s -> s == null).count();
ImmutableSet<Predicate<String>> testIsNullFunction() {
return ImmutableSet.of(s -> s == null, not(Objects::nonNull));
}

long testNonNullFunction() {
return Stream.of("foo").filter(s -> s != null).count();
ImmutableSet<Predicate<String>> testNonNullFunction() {
return ImmutableSet.of(s -> s != null, not(Objects::isNull));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import static java.util.Objects.requireNonNullElse;
import static java.util.Objects.requireNonNullElseGet;
import static java.util.function.Predicate.not;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.function.Predicate;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class NullRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(MoreObjects.class, Optional.class);
return ImmutableSet.of(MoreObjects.class, Optional.class, not(null));
}

ImmutableSet<Boolean> testIsNull() {
Expand All @@ -32,11 +33,11 @@ String testRequireNonNullElseGet() {
return requireNonNullElseGet("foo", () -> "bar");
}

long testIsNullFunction() {
return Stream.of("foo").filter(Objects::isNull).count();
ImmutableSet<Predicate<String>> testIsNullFunction() {
return ImmutableSet.of(Objects::isNull, Objects::isNull);
}

long testNonNullFunction() {
return Stream.of("foo").filter(Objects::nonNull).count();
ImmutableSet<Predicate<String>> testNonNullFunction() {
return ImmutableSet.of(Objects::nonNull, Objects::nonNull);
}
}

0 comments on commit d11ac09

Please sign in to comment.