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

Improve IsInstanceLambdaUsage check #401

Merged
merged 3 commits into from
Dec 9, 2022
Merged

Conversation

Ptijohn
Copy link
Contributor

@Ptijohn Ptijohn commented Dec 8, 2022

By checking isinstanceof usages only against identifiers.

Fixes this issue.

@Ptijohn Ptijohn requested a review from rickie December 8, 2022 11:46
@github-actions
Copy link

github-actions bot commented Dec 8, 2022

  • Surviving mutants in this change: 2
  • Killed mutants in this change: 4
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.IsInstanceLambdaUsage 2 4

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

github-actions bot commented Dec 8, 2022

  • Surviving mutants in this change: 2
  • Killed mutants in this change: 4
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.IsInstanceLambdaUsage 2 4

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

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. Suggested commit message:

Improve `IsInstanceLambdaUsage` check (#401)

Fixes #399.

@@ -23,6 +23,7 @@ void identification() {
" // BUG: Diagnostic contains:",
" Stream.of(1).filter(i -> i instanceof Integer);",
Copy link
Member

Choose a reason for hiding this comment

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

The following expression is also flagged, while it shouldn't:

Stream.of(1).filter(i -> someOtherLocalVariable instanceof Integer);

Similarly, we should not flag BiPredicates such as in:

Flux.just(1, "foo").distinctUntilChanged(v -> v, (a, b) -> a instanceof Integer);

Copy link
Member

Choose a reason for hiding this comment

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

While there: in identification tests we often list the "good" cases first; I'll shuffle om things.

Comment on lines 42 to 52
if (tree.getKind() != Kind.LAMBDA_EXPRESSION || tree.getBody().getKind() != Kind.INSTANCE_OF) {
if (tree.getKind() != Kind.LAMBDA_EXPRESSION
|| tree.getBody().getKind() != Kind.INSTANCE_OF
|| ((InstanceOfTree) tree.getBody()).getExpression().getKind() != Kind.IDENTIFIER) {
return Description.NO_MATCH;
Copy link
Member

Choose a reason for hiding this comment

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

While we're here, let's kill the highlighted mutants.

@@ -39,7 +39,9 @@ public IsInstanceLambdaUsage() {}

@Override
public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState state) {
if (tree.getKind() != Kind.LAMBDA_EXPRESSION || tree.getBody().getKind() != Kind.INSTANCE_OF) {
if (tree.getKind() != Kind.LAMBDA_EXPRESSION
|| tree.getBody().getKind() != Kind.INSTANCE_OF
Copy link
Member

Choose a reason for hiding this comment

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

Side-remark: at some point we should take a stance around testing the Kind versus performing an instanceof check. I expect the former to be faster (didn't check), but the latter is easier to understand for new users. Also, once we set JDK 17 as a baseline we may naturally start to use the latter i.c.w. pattern matching.

Comment on lines 42 to 43
if (tree.getKind() != Kind.LAMBDA_EXPRESSION
|| tree.getBody().getKind() != Kind.INSTANCE_OF
Copy link
Member

Choose a reason for hiding this comment

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

It looks like tree.getBody().getKind() == Kind.INSTANCE_OF implies tree.getKind() == Kind.LAMBDA_EXPRESSION, so we can skip the latter check.

Copy link
Member

Choose a reason for hiding this comment

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

I was looking at this but I think this is not correct. Isn't it because we are implementing a LambdaExpressionTreeMatcher, that makes the first condition always true?

Copy link
Member

Choose a reason for hiding this comment

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

^ Discussed offline: in case of a "block lambda" we do hit this code, but tree.getBody().getKind() will then be BLOCK.

@Stephan202 Stephan202 added this to the 0.6.0 milestone Dec 9, 2022
@github-actions
Copy link

github-actions bot commented Dec 9, 2022

Looks good. All 5 mutations in this change were killed.

class surviving killed
🎉tech.picnic.errorprone.bugpatterns.IsInstanceLambdaUsage 0 5

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Ptijohn
Copy link
Contributor Author

Ptijohn commented Dec 9, 2022

Oh well, thanks a lot @Stephan202 :D
Was planning on digging on those mutants and all today, but I guess I'll learn from you instead ;) (which also works ;) )

@rickie rickie added the bug fix label Dec 9, 2022
Bastien Diederichs and others added 3 commits December 9, 2022 13:14
By checking `isinstanceof` usages only against identifiers.
@rickie rickie force-pushed the bdiederichs/is-instance-fix branch from 1220e5b to 3c2c657 Compare December 9, 2022 12:14
@rickie
Copy link
Member

rickie commented Dec 9, 2022

Rebased. Will merge once 🍏.

@rickie
Copy link
Member

rickie commented Dec 9, 2022

Oh and btw, thanks for the epic fixes @Stephan202. Shows us we need to think even more about all the edge cases.

@github-actions
Copy link

github-actions bot commented Dec 9, 2022

Looks good. All 5 mutations in this change were killed.

class surviving killed
🎉tech.picnic.errorprone.bugpatterns.IsInstanceLambdaUsage 0 5

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

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.

Thanks @Ptijohn for picking this up so fast 🚀 !

@rickie rickie merged commit 096acfb into master Dec 9, 2022
@rickie rickie deleted the bdiederichs/is-instance-fix branch December 9, 2022 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

Have IsInstanceLambdaUsage handle extra method invocations
3 participants