Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@UncleOwen
Copy link
Member

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:

  1. The rule only ever looks at the static type of an object, not at the dynamic type.
Collection<Integer> c;
String s;
c.contains(s);

This does cause a violation, because String and Integer are incompatible.

Collection<Integer> c;
Object o;
c.contains(o);

This doesn't cause a violation, because the dynamic type of o might be an Integer.

  1. The rule misses some cases where the elements themselves are generic types. Generics are difficult, so I erred on the side of simpler code. The rule is complex enough as it is.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@UncleOwen UncleOwen changed the title New rule: ThatCantBeInHere Fix #5949: [java] ThatCantBeInHere Aug 23, 2025
@UncleOwen UncleOwen added the a:new-rule Proposal to add a new built-in rule label Aug 23, 2025
@pmd-actions-helper
Copy link
Contributor

pmd-actions-helper bot commented Aug 23, 2025

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 25 errors and 8 configuration errors.

Regression Tester Report

(comment created at 2025-08-26 13:25:55+00:00 for f6fddb3)

@UncleOwen
Copy link
Member Author

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.

@UncleOwen UncleOwen force-pushed the issue-5949-new-rule-for-Collections-methods-that-take-Object-as-a-parameter branch from 0e5bb00 to d711179 Compare August 23, 2025 18:35
@jsotuyod
Copy link
Member

jsotuyod commented Aug 23, 2025

@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 ThatCantBeHere is applicable to any code that is incorrectly placed 😆 Also, without apostrophes, "cant" feels weird in a rule name.

Maybe a better name would explicitly mention collections and types… such as "CollectionTypeMismatch" or something along those lines…

@UncleOwen
Copy link
Member Author

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.

@UncleOwen
Copy link
Member Author

Interesting. I didn't think about the collection.equals(anothercollection) case.

Add support for Hashtable.contains() and ConcurrentHashMap.contains()
@UncleOwen
Copy link
Member Author

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

  1. It's out-of-scope as defined by the ticket.
  2. I think it requires different logic than the existing cases, and the rule is already complex as it is.
  3. I'm not sure, if that case is a real-life concern. If it is, we can add it if a user requests it.
  4. collection.equals(anothercollection) might be a better fit for another (hypothetical?) rule: Do we have something like EqualsOnIncompatibleTypes?

@UncleOwen UncleOwen changed the title Fix #5949: [java] ThatCantBeInHere Fix #5949: [java] CollectionTypeMismatch Aug 26, 2025
@oowekyala
Copy link
Member

I like the new name of the rule and I think you're right @UncleOwen to not include the pattern collection.equals(anothercollection) in this rule, as this might as well be a new rule that applies to any type, not just collections.

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.

@oowekyala oowekyala added this to the 7.17.0 milestone Sep 6, 2025
oowekyala added a commit that referenced this pull request Sep 6, 2025
oowekyala added a commit that referenced this pull request Sep 6, 2025
…er (#6006)

Merge branch 'issue-5949-new-rule-for-Collections-methods-that-take-Object-as-a-parameter'
@oowekyala oowekyala merged commit 9432051 into pmd:main Sep 6, 2025
12 checks passed
@UncleOwen UncleOwen deleted the issue-5949-new-rule-for-Collections-methods-that-take-Object-as-a-parameter branch September 6, 2025 13:35
@UncleOwen
Copy link
Member Author

@oowekyala Yes, if this can be simplified/generalized/improved in any way, please do so!

@adangel adangel changed the title Fix #5949: [java] CollectionTypeMismatch [java] Fix #5949: CollectionTypeMismatch Sep 8, 2025
@adangel adangel changed the title [java] Fix #5949: CollectionTypeMismatch [java] Fix #5949: New Rule: CollectionTypeMismatch Sep 12, 2025
@zbynek zbynek mentioned this pull request Oct 30, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a:new-rule Proposal to add a new built-in rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] New Rule: CollectionTypeMismatch: for Collections methods that take Object as a parameter

3 participants