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 IdentityConversion BugPattern #5

Closed
wants to merge 2 commits into from

Conversation

rickie
Copy link
Member

@rickie rickie commented Dec 7, 2021

No description provided.

@rickie rickie changed the title Introduce BugPattern IdentityConversion Introduce IdentityConversion BugPattern Jan 2, 2022
@rickie rickie requested a review from werli January 2, 2022 13:35
@rickie rickie force-pushed the rossendrijver/unwrap_unnecessary_expression branch from 5ab5742 to abf7987 Compare January 2, 2022 13:37
@rickie
Copy link
Member Author

rickie commented Jan 2, 2022

Rebased and also updated the year to 2022 ;).

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.

Added a commit, but did not yet update the tests. I wonder how likely it is that (given the new insights) this PR will be accepted upstream. Perhaps we should instead just incorporate this into Error Prone Support, for now? (We can of course just ask, either now or later.)

If we do go for Error Prone Support then we'd write the tests slightly differently, which is why I didn't change those yet.


@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!IS_CONVERSION_METHOD.matches(tree, state) && tree.getArguments().size() != 1) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!IS_CONVERSION_METHOD.matches(tree, state) && tree.getArguments().size() != 1) {
if (!IS_CONVERSION_METHOD.matches(tree, state) || tree.getArguments().size() != 1) {

This also shows we should have better test coverage 👀

Next to that, the second check is cheaper, so I'd swap them:

Suggested change
if (!IS_CONVERSION_METHOD.matches(tree, state) && tree.getArguments().size() != 1) {
if (tree.getArguments().size() != 1 || !IS_CONVERSION_METHOD.matches(tree, state)) {

Comment on lines 84 to 85
List<? extends ExpressionTree> arguments = tree.getArguments();
ExpressionTree sourceTree = arguments.get(0);
Copy link
Member

Choose a reason for hiding this comment

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

As-is arguments can be inlined. But we can reuse the variable if we move it to before the if-statement.

Comment on lines +49 to +61
"com.google.common.collect.ImmutableBiMap",
"com.google.common.collect.ImmutableList",
"com.google.common.collect.ImmutableListMultimap",
"com.google.common.collect.ImmutableMap",
"com.google.common.collect.ImmutableMultimap",
"com.google.common.collect.ImmutableMultiset",
"com.google.common.collect.ImmutableRangeSet",
"com.google.common.collect.ImmutableSet",
"com.google.common.collect.ImmutableSetMultimap",
"com.google.common.collect.ImmutableSortedMap",
"com.google.common.collect.ImmutableSortedMultiset",
"com.google.common.collect.ImmutableSortedSet",
"com.google.common.collect.ImmutableTable")
Copy link
Member

Choose a reason for hiding this comment

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

These come with a number of caveats:

  • Except for ImmutableTable they all have logic to handle partial views: in those cases a copy is made, which can be important for garbage collection purposes.
  • If the input is a *Sorted* (sub)type then the result might be a copy with different semantics. See ImmutableMap.copyOf and ImmutableSet.copyOf for subtle examples, but this is even clearer for e.g. ImmutableSortedSet.copyOf, which will resort the input according to the elements' natural order if necessary.
  • In case of ImmutableSetMultimap.copyOf the value comparator is also considered.

I don't think this means we shouldn't include these methods, but perhaps we should perhaps provide an alternative suggested fix with a suppression.

BugCheckerRefactoringTestHelper.newInstance(IdentityConversion.class, getClass());

@Test
public void removeValueOf() {
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't seem like we need to have two tests for the different kind of conversion methods. (But if we add a second suggested fix we should test that.)

@rickie
Copy link
Member Author

rickie commented Jan 3, 2022

I'm completely fine with moving this one to Error Prone Support. Will put it on the list and also update the tests accordingly.

Changes look good!

@rickie
Copy link
Member Author

rickie commented Jan 5, 2022

Moved this PR to error-prone-support.

@rickie rickie closed this Jan 5, 2022
@Stephan202 Stephan202 deleted the rossendrijver/unwrap_unnecessary_expression branch April 5, 2022 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants