[java] A false negative about the rule RSPEC-1862

  • versions used:

    • sonarqube-9.2.2.50622
    • sonar-scanner-cli-4.6.2.2472-linux
  • error observed

    • Hi, I found a false negative about this rule RSPEC-1862. Please refer to the minimized sample below (in steps to reproduce). SonarQube should have reported a warning at line 6 because the if and elif conditional expressions are same, but it doesn’t.
  • steps to reproduce

  1. Detect the following case by SonarQube and Sonar-Scanner
public class Test {
  public void foo(int param) {
    if (param == 1)
        System.out.println(1);
    else if (1 == param)  //  should report a warning here, but no warnings
        System.out.println(2);
  }
}
  1. The conditional expressions in if and elif branch are same
  • potential workaround
    I think we should consider the left and right values of logical expression and leverage them to check whether conditional expressions are identical.

Hey @Belle ,

Thanks for identifying the issue and the simple reproducer! We appreciate it!

I agree that such simple cases should be covered by the rule, as well as other cases that I identified while looking at your case and the implementation of the rule, such as, for instance:

public class A {
  void bar(boolean a) {
    if (a)
        System.out.println(1);
    else if ((a)) // FN S1862 - useless parenthesis forgotten?
        System.out.println(2);
  }
}

I created the following ticket to track the issue and eventually improve the rule: SONARJAVA-4111

Cheers,
Michael

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