-
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 CollectorMutability
check
#135
Conversation
ead1bdc
to
32c976c
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.
Have training right now, so already flushing the comments I have so far. PTAL :).
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheckTest.java
Outdated
Show resolved
Hide resolved
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheckTest.java
Outdated
Show resolved
Hide resolved
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheckTest.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheckTest.java
Outdated
Show resolved
Hide resolved
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheckTest.java
Outdated
Show resolved
Hide resolved
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheckTest.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
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 some suggestions and ideas. Let me know what you think :).
Nice work already, seeing these tests makes me happy. It'll have some nice impact downstream! 🚀
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheckTest.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
linkType = NONE, | ||
severity = ERROR, | ||
tags = LIKELY_ERROR) | ||
public final class CollectorMutabilityCheck extends BugChecker |
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'm thinking about the name of the checker. Not too strong of an opinion yet but; we could also name it CollectorImmutabilty`, because we prefer it being immutable. However, AFAIK the current name doesn't mention that we prefer either one of the two. What are your thoughts 😄?
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 feel like the name is fine like this, as we want emphasis on the (im)mutability of the resulting collection.
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
504de22
to
7d7b536
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.
Rebased and added a commit. Tnx for picking this up!
Alternative suggested commit message:
Introduce `CollectorMutability` check (#135)
This check flags `Collectors.to{List,Map,Set}` usages, suggesting that they be
replaced with expressions that clearly indicate the (im)mutability of the
resultant collection.
Refaster templates that violated the new check are simplified by removing the
relevant expressions; they were too contrived to be updated some other way.
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
ExpressionTree valueMapper = tree.getArguments().get(1); | ||
|
||
return buildDescription(tree) | ||
.addFix(SuggestedFixes.renameMethodInvocation(tree, "toImmutableMap", state)) |
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.
Here and above: IIUC we may also need to introduce a (static) import.
private Description buildSimpleDescription( | ||
MethodInvocationTree tree, VisitorState state, String immutable, String mutable) { | ||
return buildDescription(tree) | ||
.addFix(SuggestedFixes.renameMethodInvocation(tree, immutable, state)) |
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.
Here and below: this breaks if the original code didn't statically import to{List,Map,Set}
. (Will add a test case.)
staticMethod().onClass("java.util.stream.Collectors").namedAnyOf("toList", "toSet"), | ||
allOf( | ||
staticMethod().onClass("java.util.stream.Collectors").named("toMap"), | ||
argumentCount(2)))); |
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 convert the three-argument case.
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
name = "CollectorMutability", | ||
summary = "`#collect(to{List,Map,Set}())` doesn't emphasize (im)mutability", | ||
linkType = NONE, | ||
severity = ERROR, |
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 guess WARNING
is more appropriate. (Within the Picnic context this doesn't matter, as we build with -Werror
.)
summary = "`#collect(to{List,Map,Set}())` doesn't emphasize (im)mutability", | ||
linkType = NONE, | ||
severity = ERROR, | ||
tags = LIKELY_ERROR) |
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.
Or FRAGILE_CODE
? 🤔
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheckTest.java
Outdated
Show resolved
Hide resolved
/** | ||
* A {@link BugChecker} which flags {@link Collector} usages that don't clearly express | ||
* (im)mutability. | ||
*/ |
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 also have a few violations of this checker in the current repo; will add a commit to resolve those. (In this case IMHO simply dropping the relevant Refaster templates seems appropriate.)
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CollectorMutabilityCheck.java
Outdated
Show resolved
Hide resolved
(Also verified that the code now has 100% mutation coverage.) |
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.
Nice feedback @Stephan202.
The code LGTM!
Pushed some changes, we could further cleanup some of the tests.
Tnx for the extra cleanup @rickie! Feel free to merge if no further changes are planned. |
collect(to{List,Map,Set}())
to emphasize (im)mutabilityCollectorMutability
check
collectingAndThen(null, null), | ||
naturalOrder(), | ||
toList()); | ||
Arrays.class, Collections.class, Comparator.class, Streams.class, naturalOrder()); |
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.
@rickie another thought I had (and this one will be tricky to implement, I suspect, so not high on the list): we should also be able to automatically validate that elidedTypesAndStaticImports
is minimal. (And perhaps sorted?)
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.
Nice, will write it down.
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.
😄
Implement BugPattern to flag
collect(to{List,Map,Set}())
and suggest to either use an Immutable variant or a clear mutable variant, in order to emphasise the (im)mutability of the resulting collection.Example:
Suggested commit message: