-
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
PrimitiveComparison: Retain type arguments if present #39
Merged
Stephan202
merged 4 commits into
master
from
rossendrijver/primitive_comparison_type_arguments
Apr 11, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; | ||
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; | ||
import static java.util.function.Predicate.not; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.common.base.VerifyException; | ||
|
@@ -30,14 +31,14 @@ | |
import java.util.Comparator; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* A {@link BugChecker} which flags {@code Comparator#comparing*} invocations that can be replaced | ||
* with an equivalent alternative so as to avoid unnecessary (un)boxing. | ||
*/ | ||
// XXX: Add more documentation. Explain how this is useful in the face of refactoring to more | ||
// specific types. | ||
// XXX: Change this checker's name? | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "PrimitiveComparison", | ||
|
@@ -77,23 +78,53 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState | |
} | ||
|
||
return getPotentiallyBoxedReturnType(tree.getArguments().get(0)) | ||
.flatMap(cmpType -> tryFix(tree, state, cmpType, isStatic)) | ||
.flatMap(cmpType -> attemptMethodInvocationReplacement(tree, cmpType, isStatic, state)) | ||
.map(fix -> describeMatch(tree, fix)) | ||
.orElse(Description.NO_MATCH); | ||
} | ||
|
||
private static Optional<Fix> tryFix( | ||
MethodInvocationTree tree, VisitorState state, Type cmpType, boolean isStatic) { | ||
private static Optional<Fix> attemptMethodInvocationReplacement( | ||
MethodInvocationTree tree, Type cmpType, boolean isStatic, VisitorState state) { | ||
return Optional.ofNullable(ASTHelpers.getSymbol(tree)) | ||
.map(methodSymbol -> methodSymbol.getSimpleName().toString()) | ||
.flatMap( | ||
actualMethodName -> | ||
Optional.of(getPreferredMethod(state, cmpType, isStatic)) | ||
Optional.of(getPreferredMethod(cmpType, isStatic, state)) | ||
.filter(not(actualMethodName::equals))) | ||
.map( | ||
preferredMethodName -> | ||
prefixTypeArgumentsIfRelevant(preferredMethodName, tree, cmpType, state)) | ||
.map(preferredMethodName -> suggestFix(tree, preferredMethodName, state)); | ||
} | ||
|
||
private static String getPreferredMethod(VisitorState state, Type cmpType, boolean isStatic) { | ||
/** | ||
* Prefixes the given method name with generic type parameters if it replaces a {@code | ||
* Comparator#comparing{,Double,Long,Int}} method which also has generic type parameters. | ||
* | ||
* <p>Such type parameters are retained as they are likely required. | ||
* | ||
* <p>Note that any type parameter to {@code Comparator#thenComparing} is likely redundant, and in | ||
* any case becomes obsolete once that method is replaced with {@code | ||
* Comparator#thenComparing{Double,Long,Int}}. Conversion in the opposite direction does not | ||
* require the introduction of a generic type parameter. | ||
*/ | ||
private static String prefixTypeArgumentsIfRelevant( | ||
String preferredMethodName, MethodInvocationTree tree, Type cmpType, VisitorState state) { | ||
if (tree.getTypeArguments().isEmpty() || preferredMethodName.startsWith("then")) { | ||
return preferredMethodName; | ||
} | ||
|
||
String typeArguments = | ||
Stream.concat( | ||
Stream.of(Util.treeToString(tree.getTypeArguments().get(0), state)), | ||
Stream.of(cmpType.tsym.getSimpleName()) | ||
.filter(u -> "comparing".equals(preferredMethodName))) | ||
.collect(joining(", ", "<", ">")); | ||
|
||
return typeArguments + preferredMethodName; | ||
} | ||
|
||
private static String getPreferredMethod(Type cmpType, boolean isStatic, VisitorState state) { | ||
Types types = state.getTypes(); | ||
Symtab symtab = state.getSymtab(); | ||
|
||
|
@@ -128,9 +159,6 @@ private static Optional<Type> getPotentiallyBoxedReturnType(ExpressionTree tree) | |
} | ||
} | ||
|
||
// XXX: We drop explicitly specified generic type information. In case the number of type | ||
// arguments before and after doesn't match, that's for the better. But if we e.g. replace | ||
// `comparingLong` with `comparingInt`, then we should retain it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests don't show such a replacement. Is this a realistic scenario that we (will) handle with this check? |
||
private static Fix suggestFix( | ||
MethodInvocationTree tree, String preferredMethodName, VisitorState state) { | ||
ExpressionTree expr = tree.getMethodSelect(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Did not improve the naming, but suggest that we either improve the naming or delete this XXX 😄.
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 keep the comment since it may still apply 🙃
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.
To shortly clarify, I did think about the XXX but think that the name is fine as it is.
So I removed the line to "start the discussion" because I feel that we can easily do the XXX by either removing it or coming up with an improvement 😄.
Some ideas:
ComparatorUsage
,ComparatorPrimitive
,CanonicalComparatorUsage
.WDYT?
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.
ComparatorUsage
andCanonicalComparatorUsage
are nice, but hint at something more generic. Let's keep the current name, at least in the context of the current PR.