Skip to content

Commit

Permalink
Introduce OptionalEmpty Refaster rule
Browse files Browse the repository at this point in the history
  • Loading branch information
werli authored and rickie committed Apr 30, 2024
1 parent e7d50c2 commit 1d89cc6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
final class OptionalRules {
private OptionalRules() {}

/** Prefer {@link Optional#empty()} over the more contrived alternative. */
static final class OptionalEmpty<T> {
@BeforeTemplate
Optional<T> before() {
return Optional.ofNullable(null);
}

@AfterTemplate
Optional<T> after() {
return Optional.empty();
}
}

static final class OptionalOfNullable<T> {
// XXX: Refaster should be smart enough to also rewrite occurrences in which there are
// parentheses around the null check, but that's currently not the case. Try to fix that.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Streams.class);
}

Optional<String> testOptionalEmpty() {
return Optional.ofNullable(null);
}

ImmutableSet<Optional<String>> testOptionalOfNullable() {
return ImmutableSet.of(
toString() == null ? Optional.empty() : Optional.of(toString()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Streams.class);
}

Optional<String> testOptionalEmpty() {
return Optional.empty();
}

ImmutableSet<Optional<String>> testOptionalOfNullable() {
return ImmutableSet.of(Optional.ofNullable(toString()), Optional.ofNullable(toString()));
}
Expand Down

0 comments on commit 1d89cc6

Please sign in to comment.