java:S1905 and overloading

The description of java:S1905, which flags redundant casts, mentions that casts may still be necessary to disambiguate when there is overloading.

Does the rule check for the existence of overloading? In other words, if SQ flags a cast which is actually necessary because of overloading (so if I remove it like SQ says then I’ll get a different method call), should I regard that as FP that should be reported? Or is the expectation that this is normal, and one should always check first before deleting?

To put it another way: if SQ tells me a particular cast is redundant, can I (or a script) just blindly remove it, or do I still need to check?

Hello @MisterPi , thanks for contributing to the Sonar community.

You can report it as FP in that particular case when you have overloading.
If you mean “just blindly remove [the cast]” no, I don’t recommend it because in some cases you need it.

Not sure if I get your answer. Are you saying:

  1. SQ tries to detect the cases where you HAVE to have the case to resolve ambiguity, but doesn’t get it right all the time (so you’d better check just to be sure), or
  2. SQ doesn’t check for overloading, so these are just hints

What I mean is that SQ does not consider whether the method calls with the cast points to an overloaded method. So, in these cases, you will have an FP.

So, your point 2.

I have created a jira ticket in order to explore the improvement of this rule.

1 Like

OK, thanks for clearing that up. I was thinking of just having a script remove the redundant casts, but that could be dangerous.

IDK whether your analyzer is fully capable of detecting overloading in all cases. Maybe the best you can do is have it bucket the issues into 3 cases:

  1. Overloading detected so the cast IS needed (so don’t flag this)
  2. There is definitely no overloading, so this cast is definitely redundant
  3. Can’t tell whether there is overloading, so this case MIGHT be redundant

Not sure how you would mark 2 and 3 differently. Maybe use the message field? Something like “Remove this unnecessary cast to “foo”.” for case 2 vs. “The cast to “foo” might be redundant” for case 3.

In any case, the rule description should be clear that SQ does NOT actually check that the cast is redundant.

So I took a look at that Jira page. Actually, it appears that SonarLint running on IntelliJ IDEA DOES distinguish between the two cases. I pasted your code into IJ and got this:
Note the highlighting in the first cast but not the second (a bit dim with this dark mode).

Now if I comment out the second definition of fun() in line 6 (numbered according to the Jira page), then the second cast is also flagged (as expected):

(Interestingly, when I looked at it a second time, the popup changed:
It seems IJ and SL are fighting over who gets to complain. :slight_smile:

So I’m not sure if what you asked for in the Jira page is already present (at least SOMEtimes), or if SL is taking advantage of IJ’s own analysis.