-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[java] Fix #5949: New Rule: CollectionTypeMismatch #6006
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
[java] Fix #5949: New Rule: CollectionTypeMismatch #6006
Conversation
|
Compared to main: (comment created at 2025-08-26 13:25:55+00:00 for f6fddb3) |
|
At first I thought the violations in openjdk-11 (from the regression test) were openjdk doing strange things. Now I had a closer look and it was indeed something I missed. |
0e5bb00 to
d711179
Compare
|
@UncleOwen this is really cool. I remember back in the day Findbugs used to have a similar rule… I really like having this here. And certainly the work we did on type res in PMD 7 now makes it significantly easier to manage. Thanks for the PR! The one thing I don't really like is the name… we should aim for rule names to be as self-descriptive as possible (within a constrained length), and Maybe a better name would explicitly mention collections and types… such as "CollectionTypeMismatch" or something along those lines… |
|
Yes, please consider all my naming open for diskussion :) I'll wait a day or two if someone comes up with other naming ideas, but CollectionTypeMismatch is definitely better than my idea. |
|
Interesting. I didn't think about the collection.equals(anothercollection) case. |
Add support for Hashtable.contains() and ConcurrentHashMap.contains()
|
I've added the tests from findbugs and made some minor additions because of that. Rule is renamed to CollectionTypeMismatch. I don't think I want to add support for the collection.equals(anothercollection) case right now, because
|
|
I like the new name of the rule and I think you're right @UncleOwen to not include the pattern I was thinking there may be a better approach than implementing a new "compatibility" routine in this rule, one that would account for the generic cases you mention are hard. The notion of "compatibility" we want here is, given types A and B, they are compatible if there is there may be some type C that is a subtype of both A and B. The simplest way to test for this and account for all corner cases related to generics is to treat that as a type inference problem. We can create an inference variable, add two constraints saying that it must subclass A and subclass B, and then solve for the variable. If A and B are incompatible, inference will fail. I have implemented this approach locally and it works well. I am happy to merge this PR as is into main and propose my changes as a follow-up PR. |
…er (#6006) Merge branch 'issue-5949-new-rule-for-Collections-methods-that-take-Object-as-a-parameter'
|
@oowekyala Yes, if this can be simplified/generalized/improved in any way, please do so! |
Describe the PR
This PR creates a new rule. This new rule creates a violation, if a Collection is queried for an element that cannot possibly be in there due to type mismatch.
Known limitations:
This does cause a violation, because String and Integer are incompatible.
This doesn't cause a violation, because the dynamic type of o might be an Integer.
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)