C & C++

C & C++

By DeepSource

Suspicious placement of semicolon hinting difference in code behaviour and programmer's intent CXX-A1003

Bug risk

Using a semicolon right after the conditional statement (for example an if, for, while, etc.) indicates that nothing happens if the predicate is true. Though this is sometimes intentional, but at other times this might also indicate a possible miss by the programmer, specially when the following lines of code have specific traits (like indentation matching that of the conditional block).

If the semicolon after the conditional statements is intentional, consider moving the semicolon to the next line or indent the following code block/line with that of the conditional.

Bad practice

Bug-prone semicolon

while ((l = readLine(fileHandle)) != NULL); // unintended semicolon
    comsumeLine(l);

Intentional semicolon

while ((skipSpaces(line));
    readLine(line);

Recommended

while ((l = readLine(fileHandle)) != NULL)
    comsumeLine(l);

while ((skipSpaces(line));
readLine(line);

// OR 

while ((skipSpaces(line))
    ;
readLine(line);