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 Mono{Empty,Just,JustOrEmpty} Refaster rules #385

Merged
merged 3 commits into from
Dec 6, 2022

Conversation

werli
Copy link
Member

@werli werli commented Dec 4, 2022

After #384 I noticed a few more interactions between the Optional and Mono API we can simplify.

Pretty simple cases, but why not cover them?


Suggested commit message:

Introduce `Mono{Empty,Just,JustOrEmpty}` Refaster rules (#385)

@github-actions
Copy link

github-actions bot commented Dec 4, 2022

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

static final class MonoEmpty<T> {
@BeforeTemplate
Mono<T> before() {
return Refaster.anyOf(Mono.just(null), Mono.justOrEmpty(null));
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm admittedly not fully sure whether we should actually cover Mono.just(null), so I added it as part of an extra commit. It's such an obvious mistake that a Bug Check may make more sense.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think we should indeed omit this one, since Mono.just(null) is actually a bug, triggering an NPE. Ideally NullAway would flag it (and we should look into enabling NullAway internally).

OTOH, the suggested fix for Mono.just(null) is likely Mono.empty(), so in that sense its inclusion may make sense 🤷.

For now I'll push a commit which replaces it with another applicable case: Mono.justOrEmpty(Optional.empty()).

Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Added a commit. Updated suggested commit message:

Introduce `Mono{Empty,Just,JustOrEmpty}` Refaster rules (#385)

static final class MonoEmpty<T> {
@BeforeTemplate
Mono<T> before() {
return Refaster.anyOf(Mono.just(null), Mono.justOrEmpty(null));
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think we should indeed omit this one, since Mono.just(null) is actually a bug, triggering an NPE. Ideally NullAway would flag it (and we should look into enabling NullAway internally).

OTOH, the suggested fix for Mono.just(null) is likely Mono.empty(), so in that sense its inclusion may make sense 🤷.

For now I'll push a commit which replaces it with another applicable case: Mono.justOrEmpty(Optional.empty()).

Comment on lines 78 to 81
@BeforeTemplate
@SuppressWarnings("NullAway")
Mono<T> before(@Nullable T value) {
return Mono.justOrEmpty(Refaster.anyOf(Optional.of(value), Optional.ofNullable(value)));
}
Copy link
Member

Choose a reason for hiding this comment

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

It seems like we can avoid @SuppressWarnings("NullAway") two ways:

  1. Moving the Optional.of(value) to a separate @BeforeTemplate method.
  2. Just omit the @Nullable annotations, since they don't impact Refaster anyway.

Right now (2) seems simplest. If later we flag Optional.ofNullable(v) for non-@Nullable values v, then we should use (1).

Copy link
Member

Choose a reason for hiding this comment

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

But... actually we should have a separate rule for Mono.just, which gives us (1) for free :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool stuff, even better. 🚀

@Stephan202 Stephan202 added this to the 0.6.0 milestone Dec 4, 2022
@Stephan202 Stephan202 requested a review from rickie December 4, 2022 12:56
@github-actions
Copy link

github-actions bot commented Dec 4, 2022

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member Author

@werli werli left a comment

Choose a reason for hiding this comment

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

Changes LGTM, thx @Stephan202. Make sense.

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

Nice improvements @werli 🚀 !

You know what this means 😉 ==> 🎄 🎅🏻 :cody:

@rickie rickie force-pushed the werli/mono-empty-rules branch from 0c9d9c3 to 27f5312 Compare December 5, 2022 17:35
@rickie
Copy link
Member

rickie commented Dec 5, 2022

Rebased. Will merge once 🟢.

@rickie
Copy link
Member

rickie commented Dec 6, 2022

Rebased. Will merge once 🟢.

Take two!

@rickie rickie force-pushed the werli/mono-empty-rules branch from 27f5312 to 7f9ec03 Compare December 6, 2022 07:07
@github-actions
Copy link

github-actions bot commented Dec 6, 2022

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie changed the title Introduce Mono{JustOr}Empty Refaster rules Introduce Mono{Empty,Just,JustOrEmpty} Refaster rules Dec 6, 2022
@rickie rickie merged commit 1afce12 into master Dec 6, 2022
@rickie rickie deleted the werli/mono-empty-rules branch December 6, 2022 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants