Download LCLint User's Guide

Transcript
48
LCLint User’s Guide
Four types of break errors are reported:
• break inside a loop (while or for) that is inside a loop. Controlled by looploopbreak. To
indicate that a break is inside an inner loop, precede the break by /*@innerbreak@*/.
• break inside a loop that is inside a switch statement. Controlled by switchloopbreak. To
mark the break as a loop break, precede the break by /*@loopbreak@*/.
• break inside a switch statement that is inside a loop. Controlled by loopswitchbreak. To
mark the break as a switch break, precede the break by /*@switchbreak@*/.
• break inside a switch inside another switch. Controlled by switchswitchbreak. To
indicate that the break is for the inner switch, use /*@innerbreak@*/.
Since continue only makes sense within loops, errors are only reported for continue statements
within nested loops. (Controlled by looploopcontinue.) A safe inner continue may be
preceded by /*@innercontinue@*/ to suppress error messages locally. The deepbreak flag
sets all nested break and continue checking flags.
LCLint reports an error if the marker preceding a break is not consistent with its effect. An error is
reported if innerbreak precedes a break that is not breaking an inner loop, switchbreak
precedes a break that is not breaking a switch, or loopbreak precedes a break that is not
breaking a loop.
10.2.4 Loop and If Bodies
An empty statement after an if, while or for often indicates a potential bug. A single statement
(i.e., not a compound block) after an if, while or for is not likely to indicate a bug, but make the
code harder to read and edit. LCLint can report errors for if or loop statements with empty bodies or
bodies that are not compound statements. Separate flags control checking for statements following an
if, while or for:
• [if, while, for]empty — report errors for empty bodies (e.g., if (x > 3) ; )
• [if, while, for]block — report errors for non-block bodies (e.g., if (x > 3) x++;)
The if statement checks also apply to the body of the else clause. An ifblock error is not
reported if the body of the else clause is an if statement, to allow else if chains.
10.2.5 Complete if-else Logic
Although it may be perfectly reasonable in many contexts, an if-else chain with no final else may
indicate missing logic or forgetting to check error cases. If elseif-complete is on, LCLint
reports errors when an if statement that is the body of an else clause does not have a matching
else clause. For example, the code,
if (x == 0) { return "nil"; }
else if (x == 1) { return "many"; }
produces an error message since the second if has no matching else branch.
10.3 Suspicious Statements
LCLint detects errors involving statements with no apparent effects (Section 10.3.1) and statements
that ignore the result of a called function (Section 10.3.2).
10.3.1 Statements with No Effects
LCLint can report errors for statements that have no effect. (Controlled by no-effect.) Because
of modifies clauses, LCLint can detect more errors than traditional checkers. Unless the no-