Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering
QBA: Method assigns boolean literal in boolean expression
This method assigns a literal boolean value (true or false) to a boolean variable inside an if or while expression. Most probably, this was supposed to be a boolean comparison using ==, not an assignment using =.
We expect in a condition that two or more variables compare to each other and then a true/false answer will be returned. Typically, one would use a compression operation like “==”. However, this can easily be mistyped as “=”, which is an assignment operator and returns always “true”. This likely error is not detected by the Java compiler.
In our implementation, we go beyond FindBugs in that we detect any assignment of a boolean expression inside a condition. Note that there is no need to detect assignments of other types inside conditions, because those are caught already by the Java compiler (e.g. a=5 is not a boolean expression).
<code Java> boolean a=true; boolean b=true; if (a=b) {
System.out.println("There should be == instead of =");
}
while (a=b) {
System.out.println("There should be == instead of =");
} *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+* More generally: while (… var=boolean expression …) {
statement(s)
} if (… var=boolean expression …) {
statement(s)
} </Code>
<code Java>
static void find() { Boolean Ob = false; while( Ob ==true){ break; } }
</Code>
<code Java> boolean a=true; boolean b=true; if (a==b) {
System.out.println("There should be == instead of =");
}
while (a==b) {
System.out.println("There should be == instead of =");
} </Code>
Benchmark project | Precision | Recall | ||||
---|---|---|---|---|---|---|
FB | JT | Delta | FB | JT | Delta | |
Project … | …% | …% | …% | …% | …% | …% |
Project … | …% | …% | …% | …% | …% | …% |
FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBugs