Cannot understand SonarQube issue rationale "Change this condition so it does not evaluate to false"

Dear all,
I have analyzed a java project and SonarQube has found a bug that I cannot understand. I get the error “Change this condition so it does not evaluate to false” (see code snippet)

I do not understand why SonarQube says in number 1 “verbose implies false”… Is it saying that the variable is “false” since its instantiation, when it executes the statement Option.isVerbose(option)? Or it is referring to the first clause if, that somehow the code never gets past it when verbose is true?

Thank you for your help.

PS: I have been checking other posts, but not found any similarities.

1 Like

Hi there,
For further information, the scanner used for the analysis is sonar-maven-plugin:3.9.1.2184:sonar (default-cli).

Hi,

What version of SonarQube are you using?

 
Thx,
Ann

Hi there,
We are using sonarqube latest 10.1. Best regards.

Hi,

Thanks for the confirmation. I’ve flagged this for the appropriate folks.

 
Ann

Hello @mvillanueva,

I tried to reproduce your scenario:

  boolean isVerbose(int options) {
    return false;
  } 

  public void preprocessFile(int options) {
    boolean verbose = isVerbose(options);

    if(verbose) { // Compliant
      System.out.println("Is Verbose");
    }
  }

It seems that the rule can infer that verbose is false from the return of a static method; in the example the isVerbose method.

Can you double-check that Option.isVerbose is not returning always false? If you prefer you can point me to the Option.isVerbose code.

As a counter-example, the rule won’t be able to infer anything in the case of isVerbose2 because its return value depends on the input:

  public static void preprocessFile2(int options) {
    boolean verbose = isVerbose2(options);

    if(verbose) { // Compliant
      System.out.println("Is Verbose");
    }
  }

  static boolean isVerbose2(int options) {
    return otpions == 1;
  }

Cheers

1 Like

Dear Angelo, thank you for your response. I completely understand the two scenarios that you provided, but I believe our method does not always return false, it depends on the input (it performs a binary operation).

public static final int VERBOSE = 1<<2;

public static boolean isVerbose(int options) {
return (options & VERBOSE) != 0;
}

Best regards, and thank you again for your help.

1 Like

Thank you @mvillanueva for providing the missing code:

  public static final int VERBOSE = 1<<2;

  static boolean isVerbose(int options) {
    return (options & VERBOSE) != 0;
  } 

It seems that your options integer is treated as a bitmask and the VERBOSE constant is mapped on the 3rd least significant (rightmost) bit; this means that the isVerbose(int options) returns true when such bit is set.

When I unit-test the following code, no issue is raised by the rule, so everything seems to be fine.

  public static final int VERBOSE = 1<<2;

  static boolean isVerbose(int options) {
    return (options & VERBOSE) != 0;
  } 

  public static void preprocessFile(int options) {
    boolean verbose = isVerbose(options);

    if(verbose) { // Compliant
      System.out.println("Is Verbose");
    }
  }

I’m not able to reproduce this scenario. Let me make sure I’m looking at the correct rule, can you confirm that the issue was raised by rule S2583?

1 Like

Yes, that is the rule.
Best regards and thank you.