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

fix #1025 Error Prone StrictCollectionIncompatibleType to guard collection usage #1027

Merged
merged 5 commits into from
Nov 7, 2019

Conversation

carterkozak
Copy link
Contributor

This check is an improvement over CollectionIncompatibleType because it
validates that values exist in the same type hierarchy, where it could
theoretically be possible the input implements the collection type,
but the type system doesn't have enough information to be confident.
This check allows both subtypes and supertypes, but does not check
for shared supertypes, which are not common.

After this PR

==COMMIT_MSG==
Error Prone StrictCollectionIncompatibleType to guard collection usage
==COMMIT_MSG==

…lection usage

This check is an improvement over `CollectionIncompatibleType` because it
validates that values exist in the same type hierarchy, where it could
theoretically be possible the input implements the collection type,
but the type system doesn't have enough information to be confident.
This check allows both subtypes and supertypes, but does not check
for shared supertypes, which are not common.
@changelog-app
Copy link

changelog-app bot commented Nov 6, 2019

Generate changelog in changelog/@unreleased

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Error Prone StrictCollectionIncompatibleType to guard collection usage

Check the box to generate changelog(s)

  • Generate changelog entry

@TheBeruriahIncident
Copy link
Contributor

Just tested locally. This does not catch the bug that inspired me to file the ticket, because this does not catch method references. However, it seems to catch the simpler case.
I.e. the case I had was roughly:

.map(map::get)

which is not caught, but this is:

.map(key -> map.get(key))

@carterkozak is aware and prefers to stick with this at first, then support method references if this doesn't cause problems.

@carterkozak
Copy link
Contributor Author

Verified on a large internal project. Three failures, all of which appear to be legitimate.
Working on a follow-up to flag method references.

altNames = {"SuspiciousMethodCalls", "CollectionIncompatibleType"},
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks",
linkType = BugPattern.LinkType.CUSTOM,
severity = BugPattern.SeverityLevel.WARNING,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are pretty serious, and our large internal project had no false positives. We could bump this to ERROR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's leave it WARNING for now, and bump it in a week or so.

Copy link
Contributor

@dansanduleac dansanduleac left a comment

Choose a reason for hiding this comment

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

This is pretty cool! Can we add a couple of test cases involving primitive arguments too?

Copy link
Contributor Author

@carterkozak carterkozak left a comment

Choose a reason for hiding this comment

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

What sort of additional testing for primitives are you looking for, @dansanduleac?

" }",
" boolean f4(List<Integer> in) {",
" // BUG: Diagnostic contains: incompatible types",
" return in.remove(5L);",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This tests boxing the wrong primitive results in a failure

Copy link
Contributor

Choose a reason for hiding this comment

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

oh somehow glossed over these, I was scanning for a function with a type of int or something

" return in.contains(null);",
" }",
" boolean f1(Collection<Integer> in) {",
" return in.contains(3);",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests boxing the expected primitive works as expected.

@carterkozak
Copy link
Contributor Author

dozer?

@bulldozer-bot bulldozer-bot bot merged commit 57d3cff into develop Nov 7, 2019
@bulldozer-bot bulldozer-bot bot deleted the ckozak/StrictCollectionIncompatibleType branch November 7, 2019 17:29
@svc-autorelease
Copy link
Collaborator

Released 2.29.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants