SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Cor 100 -(QBA_QUESTIONABLE_BOOLEAN_ASSIGNMENT)

QBA: Method assigns boolean literal in boolean expression

Inspired by

Original Description

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 =.

Detailed Description

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

Sample Problem Scenario

<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>

Sample Counter Scenario

<code Java>

static void find() {
	Boolean Ob = false;
	while( Ob ==true){
		
		break;
	}
	
}

</Code>

Sample Fix

<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>

Evaluation Results

Benchmark project Precision Recall
FB JT Delta FB JT Delta
Project … …% …% …% …% …% …%
Project … …% …% …% …% …% …%

FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBugs

teaching/labs/mdse/2013/bug_descriptions/jt-bug-cor100.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2023