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.
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).
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?