FP on rule S3740 when instanceof with variable is used on raw types

Description

The isntanceof with a pattern matching variable causes false positives when used on raw types and there are cases in which it is valid.
Examples

  • Not a False Positive:

in this case, the rule prevents runtime ClassCastException

  • Possibly a False Positive:

in this case, there is no runtime problem and the raw type usage can be valid

 

Notes:

  • We may want to treat the second code example as a False Positive

  • We may want to introduce a new rule to check cases like the first code example

Activity

Marco KaufmannApril 18, 2024 at 12:02 PM

The second use case is no FP either I think (just the name stringList replaced by list and former list by object):

Because we use List list without a type parameter here, and why treat it special only because it’s instanceOf? If this is allowed, then also this should be allowed, which it is not:

Marco KaufmannApril 18, 2024 at 8:54 AM

For this:

we should add a short description to the RSPEC How to fix this issue, because the proper way to fix it would NOT be:

but:

This is because instanceOf can check only against the (erased) runtime type, hence can’t assert the binding of the type parameter. I don’t know if that’s obvious, might be worth mentioning.

Michael GumowskiJune 26, 2023 at 3:10 PM

Useful links:

Done
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Components

Fix versions

Priority

Sentry

Created June 26, 2023 at 3:10 PM
Updated October 16, 2024 at 2:11 PM
Resolved April 22, 2024 at 10:08 AM