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 MonoIdentity and MonoThen Refaster rules #405

Merged
merged 2 commits into from
Dec 12, 2022

Conversation

pacbeckh
Copy link
Contributor

@pacbeckh pacbeckh commented Dec 9, 2022

We came across this pattern in our code base after some (automatic) refactorings.

mono
    .flatMap(operationReturningMonoVoid)
    .flux()
    .then();

.flux().then() doesn't add anything, similarly to calling .then on a Mono<Void>.

Introduced two rules for both steps


Suggested commit message:

Introduce `MonoFluxThen` and `MonoVoidThen` Refaster rules (#405)

@github-actions
Copy link

github-actions bot commented Dec 9, 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.

}

/** Don't unnecessarily call {@link Mono#then()} on a {@code Mono<Void>} . */
@SuppressWarnings("VoidMissingNullable")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Introduced this because I got into a fight with NullAway:

[NullAway] dereferenced expression mono is @Nullable

"mono" was nullable:

    @BeforeTemplate
    Mono<@Nullable Void> before(Mono<@Nullable Void> mono) {
      return mono.then();
    }

regardless, I wasn't really sure why we need @Nullable on the Void type in this context

Copy link
Member

Choose a reason for hiding this comment

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

IIUC this is a bug in NullAway; indeed their support for generics is limited. Let's add the @Nullable annotations (they are correct) but also keep the suppression (only on this class).

}

Mono<Void> testMonoVoidThen() {
return Mono.just("foo").then().then();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The most concise way I could think of creating a Mono<Void>, not the prettiest

Copy link
Member

Choose a reason for hiding this comment

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

How about Mono.<Void>empty()? :)

@pacbeckh pacbeckh force-pushed the pacbeckh/mono-voidthen-fluxthen branch from 7c22188 to d4145a5 Compare December 9, 2022 17:00
@github-actions
Copy link

github-actions bot commented Dec 9, 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.

@pacbeckh pacbeckh marked this pull request as ready for review December 9, 2022 17:06
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.

Thanks for your contribution @pacbeckh!

I rebased and added a commit. Perhaps the MonoIdentity rule should now be moved (we have some "fuzzy logic" for that), but as-is the diff is easier to review.

Suggested commit message:

Introduce `MonoIdentity` and `MonoThen` Refaster rules (#405)

The `MonoIdentity` rule is a generalization of the existing
`MonoSwitchIfEmptyOfEmptyPublisher` rule.

}

/** Don't unnecessarily call {@link Mono#then()} on a {@code Mono<Void>} . */
@SuppressWarnings("VoidMissingNullable")
Copy link
Member

Choose a reason for hiding this comment

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

IIUC this is a bug in NullAway; indeed their support for generics is limited. Let's add the @Nullable annotations (they are correct) but also keep the suppression (only on this class).

Comment on lines 700 to 703
@AfterTemplate
Mono<Void> after(Mono<Void> mono) {
return mono;
}
Copy link
Member

Choose a reason for hiding this comment

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

We can combine this rule with MonoSwitchIfEmptyOfEmptyPublisher above, as we generally try to have one rule for each "target shape" in an @AfterTemplate.

@@ -675,6 +675,34 @@ Flux<T> after(Mono<T> mono) {
}
}

/** Don't unnecessarily convert {@link Mono} to {@link Flux}. */
@SuppressWarnings("VoidMissingNullable")
static final class MonoFluxThen<T> {
Copy link
Member

Choose a reason for hiding this comment

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

We haven't fully migrated yet, but the naming standard we're thinking of settling on would have this rule be named MonoThen, derived from the code in the @AfterTemplate.

}

Mono<Void> testMonoVoidThen() {
return Mono.just("foo").then().then();
Copy link
Member

Choose a reason for hiding this comment

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

How about Mono.<Void>empty()? :)

@Stephan202 Stephan202 requested a review from rickie December 11, 2022 06:50
@Stephan202 Stephan202 added this to the 0.6.0 milestone Dec 11, 2022
@github-actions
Copy link

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

@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.

Changes LGTM!

Nice contribution @pacbeckh !

@rickie rickie changed the title Introduce MonoFluxThen and MonoVoidThen Refaster rules Introduce MonoIdentity and MonoThen Refaster rules Dec 12, 2022
@rickie rickie merged commit 2cbd48e into master Dec 12, 2022
@rickie rickie deleted the pacbeckh/mono-voidthen-fluxthen branch December 12, 2022 07:52
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