Difference between java:S1158 and java:S2131?

As of 8.6, there are two Java rules, on by default: java:S1158 and java:S2131. Both say not to box a primitive for the sole purpose of calling a method from the boxed type, such as toString(), because those methods are also available as static methods.

So what’s the difference between these two rules? Both descriptions show calling Integer.toString() on an int in the compliant code example. But java:S1158 also mentions compareTo(); is it just a superset of java:S2131? Both seem to be flagging a lot of the same code.

Hi Mister Pi,

Thanks for reporting this. Looking at the two different rules, they indeed seem to be targeting the same code smell. I have created a ticket for these duplicate rules to be merged into one. In the meantime, you can either disable one of the rules to avoid getting duplicate reports, or, by fixing the code smells that cause the rules trigger, you will of course fix both reports with one change.

I checked one of our projects with ~700 violations of each of these. It appears that S2131 is a subset of S1158. For every violation of S2131, there is a violation of S1158 with the same component (file path), startLine and startOffset. The endOffset fields differ; it appears S2131 includes the toString() call in the textRange field, while S1158 does not. They also have different tags: performance for 2131 and clumsy for 1158.

So on our end, we’re going to deactivate S2131, and use api/rules/update to add the performance tag to S1158.

BTW, when we do the latter, and then view the rule with api/rules/show, the new tag (performance) is in the field “tags” while the original tag (clumsy) is in the field “sysTags”. Is this to distinguish tags tied to the rule by default from tags added by the user?

As for how to do the textRange: if it were me, I would find the way S2131 marks the text range (so that the method call, e.g., toString(), is included) would be more useful than cutting that off like S1158 does. As I said here (Include surrounding parentheses in textRange for S1905) we’ve been toying around with automated fixing, having a Python or PowerShell script use the web_api to get at things.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.