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 OptionalIdentity Refaster template #245

Merged
merged 5 commits into from
Sep 19, 2022

Conversation

svavahb
Copy link
Contributor

@svavahb svavahb commented Sep 19, 2022

As suggested here.

Adds a Refaster template to prefer optional over optional.stream().findFirst().

This is redundant, as Optional#stream() makes a stream with one element, meaning the findFirst() will just return the original optional again.

(P.s. this is my first contribution, hope I did everything right 😊 And FYI the stickers are absolutely working, I'm 80% doing this for a cute lil ghost on my laptop!!! 👻)

@oxkitsune
Copy link
Contributor

Wouldn't this also be the case for:

optional.stream().findAny()

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.

Thank you for your contribution!

Rebased and added a small commit.

Suggested commit message:

Introduce `OptionalIdentity` Refaster template (#245)

* Don't use the redundant chain of {@link Optional#stream()} followed by either {@link
* Stream#findFirst()} or {@link Stream#findAny()}.
*/
abstract static class OptionalSkipStreamFindFirst<T> {
Copy link
Member

Choose a reason for hiding this comment

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

@rickie interesting case w.r.t. automated naming: we wouldn't be able to use Optional here. I'll go with OptionalIdentity for now.

static final class OptionalIdentity<T> {
@BeforeTemplate
Optional<T> before(Optional<T> optional) {
return Refaster.anyOf(optional.stream().findFirst(), optional.stream().findAny());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps we could also add stream().max() and stream().min()?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, good one! Sure; wanna add a proposal? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, will do!

Copy link
Contributor Author

@svavahb svavahb Sep 19, 2022

Choose a reason for hiding this comment

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

I'm having a small issue: The test keeps failing because the Input file has the import for java.util.Comparator.reverseOrder, while the Output file doesn't (the output doesn't use any comparator because it doesn't have the max/min function calls). It keeps suggesting to add this (unused) import:

[ERROR] Failures: 
[ERROR]   RefasterTemplatesTest.validateTemplateCollection:81 value of:
    maybeFormat(...)
diff (-expected +actual):
    @@ -1,6 +1,7 @@
     package tech.picnic.errorprone.refastertemplates;
     
     import static com.google.common.collect.Streams.stream;
    +import static java.util.Comparator.reverseOrder;
     
     import com.google.common.collect.ImmutableSet;
     import com.google.common.collect.Streams;

I can of course try to fix it by using something that doesn't require an import as the comparator, however it feels strange that it would fail on the imports? Shouldn't the import get removed from the input when the refaster template is applied in the error prone compilation?

Copy link
Member

@Stephan202 Stephan202 Sep 19, 2022

Choose a reason for hiding this comment

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

In general it's hard to know whether an import can be removed, because other code may still depend on it (and rules are applied independently). So in practice we instead rely on Google Java Format to drop unused imports after the fact. This does impose a challenge for the tests, as you have noticed. For this purpose we have that funny elidedTypesAndStaticImports method at the top of some of those test classes. But your String::compareTo usage is a nice workaround 👍.

(This does raise another point: we should have a rule that favours naturalOrder() over T::compareTo 😄 But that's for another day/PR.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, thanks for the explanation! 😄

@svavahb
Copy link
Contributor Author

svavahb commented Sep 19, 2022

Anything (else) from you @rickie or @oxkitsune ? 😊

@rickie
Copy link
Member

rickie commented Sep 19, 2022

Yes will review later today :)

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 @svavahb thanks for the nice contribution! 🚀

The sticker is coming your way tomorrow 😉.

Suggested commit message LGTM. Tried thinking about an alternative though, as we sometimes tend to use a different way of formulating commit messages for Refaster templates. Wouldn't be as nice though in this situation.

@Stephan202 Stephan202 added this to the 0.3.0 milestone Sep 19, 2022
@rickie
Copy link
Member

rickie commented Sep 19, 2022

For some reason I cannot add a label 😂 . Can one of you add the label new feature?

@Stephan202
Copy link
Member

Works on my machine ;)

@Stephan202 Stephan202 merged commit 3874ca9 into master Sep 19, 2022
@Stephan202 Stephan202 deleted the svavahb/optionals_skip_stream_findfirst branch September 19, 2022 19:20
@rickie rickie changed the title Don't use Optional#stream() followed by Stream#findFirst Introduce OptionalIdentity Refaster template Sep 20, 2022
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.

4 participants