Non-compliant code examples should violate only one rule

Rule java:S122 (multiple statements on one line) shows this example here ( SonarQube ):

if (someCondition) doSomething(); // Noncompliant

Braces are included in the compliant replacement, leading one of our devs to ask if the problem was the missing braces (but that’s a different rule). The difference between the compliant and non-compliant examples should only relate to the rule being violated. In this case, the problem was the then clause being on the same line as the conditional, but the compliant code added curlies. It would be more clear if you either removed the curlies from the compliant code or added them to the non-compliant code.

The C# and PHP versions of this rule have the same problem. The other languages seem to use a more simple case of two independent statements, which is probably a better case anyway as that is more clear about what the problem is.

(This principle should apply everywhere: the change from non-compliant code to compliant code should be the absolute minimum to make the code comply with the one specific rule being illustrated. I can’t recall any other examples off the top of my head but if I see any, I’ll post them here.)

Thank you for pointing this out! I have created a ticket, SONARJAVA-5955, to track the resolution of this issue.

Thanks! I found another one: SonarQube .

This is a slightly different animal, in that it’s not a case of the correction fixing two things at one time. But it still falls into the broad category of “inconsistency” between the compliant and non-compliant code:

  1. The non-compliant code throws an exception, but the compliant code does not. Hence, the latter is not a drop-in replacement for the former. Can I throw an exception from a switch expression? I have to look this up. (Furthermore, the switch in the non-compliant code is embedded in a function, which doesn’t seem to have any point because numLetters doesn’t “go” anywhere, while the compliant code removed the function header.)
  2. The second non-compliant code block isn’t included in the compliant code.

Thank you! I created SONARJAVA-5981 to track this issue.