-
Notifications
You must be signed in to change notification settings - Fork 39
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 StreamMapTo{Double,Int,Long}Sum
Refaster rules
#497
Conversation
Looks good. No mutations were possible for these changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the Double
and Long
variants makes sense to me :).
I added a commit; suggested commit message:
Introduce `StreamMapTo{Double,Int,Long}Sum` Refaster rules (#497)
@@ -379,4 +380,16 @@ boolean after(Stream<T> stream) { | |||
return stream.allMatch(e -> test(e)); | |||
} | |||
} | |||
|
|||
static final class StreamMapReduceIntSum<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to name rules after the code in the @AfterTemplate
, which in this case would yield:
static final class StreamMapReduceIntSum<T> { | |
static final class StreamMapToIntSum<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I was wondering whether this rule should be in the IntStreamRules
class, but since IntStream
is only an intermediate type, I suppose not.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering the same, and came to the same conclusion 🙂
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java
Show resolved
Hide resolved
Looks good. No mutations were possible for these changes. |
Forgot to say: congrats and thanks for your first Error Prone Support contribution 🎉 |
StreamMapReduceIntSum
Refaster ruleStreamMapTo{Double,Int,Long}Sum
Refaster rules
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
Thank you! 😊 I added the |
I found a related example that we used during my thesis project: google/error-prone@ I would write the Matcher to be in the same setup/style as the other matchers. Having a Matcher DELEGATE like this:
The The
This comment combined with the things we discussed offline should be a pointer 😄. |
Thank you for your time Rick! That's definitely a nice pointer, I'll take a look at it when I have some time :) |
I'm having fun! 😄 However, I am having a bit of trouble testing it in the same way as the other If I try something like this, it doesn't work because the matcher searches for matches in the whole test method, not only the Do you have any idea on how to proceed? I bet there's a way to be able to handle this by taking a look at, for example, |
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
19dc1a2
to
eaea479
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question about how to test this stuff. The trick is so split the code over multiple lines. That said, this does indeed highlight a weakness of this kind of test: it's easy to over-match.
I rebased and added a commit with some improvements. Not yet done reviewing, but thought it'd be nice to show this intermediate state :)
(Functionally I think this now looks very good; will look a bit into minimizing the test without impacting coverage.)
@@ -138,4 +139,28 @@ ImmutableSet<Boolean> testStreamAllMatch() { | |||
boolean testStreamAllMatch2() { | |||
return Stream.of("foo").noneMatch(s -> !s.isBlank()); | |||
} | |||
|
|||
ImmutableSet<Integer> testStreamMapToIntSum() { | |||
Function<String, Integer> parseIntFunction = (String s) -> Integer.parseInt(s); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just because it's shorter:
Function<String, Integer> parseIntFunction = (String s) -> Integer.parseInt(s); | |
Function<String, Integer> parseIntFunction = Integer::parseInt; |
public static <T extends Tree> Matcher<T> isMethodReference() { | ||
return (tree, state) -> | ||
tree instanceof MemberReferenceTree | ||
&& ((MemberReferenceTree) tree).getMode().equals(INVOKE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also allow ::new
, so the mode check can be omitted :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I was thinking about the specific usage of this PR, but extending it to ::new
it makes it more reusable indeed :)
public final class IsLambdaExpressionOrMethodReference implements Matcher<ExpressionTree> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private static final Matcher<ExpressionTree> DELEGATE = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While indeed usually we introduce such a DELEGATE
constant for performance reasons, in this case there's no efficiency gain over inlining the logic.
Looks good. All 5 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added one more commit. Very nice contribution @giovannizotta! 💪
Updated suggested commit message:
Introduce `StreamMapTo{Double,Int,Long}Sum` Refaster rules (#497)
As well as a new `IsLambdaExpressionOrMethodReference` matcher.
Looks good. All 5 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
Thank you @Stephan202 for the cleanup! It was the first time I dove into these concepts but I really enjoyed it, even though you ended up doing most of the work 😄 Also nice to see the intermediate state to know how to solve this kind of issue the next time :) |
Rest assured: this workflow is how it goes every time 😄. It's in the nature of the project: tricky concepts, a large and unfamiliar API, and some rather opinionated maintainers ;) If you're up for it, don't hesitate to contribute other changes if/when you observe them during code review 🚀. |
214a0c7
to
3d73f2e
Compare
Looks good. All 5 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Stephan202, I'm curious, any particular reason to not add Javadoc for the Refaster rules?
Added a commit with some suggestions.
Nice contribution @giovannizotta, cool stuff 🚀 !
"", | ||
" Predicate<String> positive1() {", | ||
" // BUG: Diagnostic contains:", | ||
" return str -> true;", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is also nice to have a lambda case with { }
, just for completeness.
Function<String, Double> parseDoubleFunction = Double::parseDouble; | ||
return ImmutableSet.of( | ||
Stream.of(1).mapToDouble(i -> i * 2.0).sum(), | ||
Stream.of("1").mapToDouble(Double::parseDouble).sum(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally use a different number or string per test case, see also further up in this file :). So let's change this.
Looks good. All 5 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
Nope, just that the preceding rules also don't have any. :) |
I came across this pattern during a code review:
.map(Stock::getQuantity).reduce(0, Integer::sum);
This felt more readable to me:
.mapToInt(Stock::getQuantity).sum();
It can also be extended to
Long
andDouble
. Let me know what you think! 🙂Suggested commit message: