FaultDetection-DeadBranches¶
There shall be no dead branches
Required inputs: IR, StaticSemanticAnalysis
For if statements and conditional operators, it reports the constant
condition itself; for loops, it reports dead loop bodies, loop
conditions that are always true, and do-while loops that execute only
once. Constant operands of the short-circuiting operators
&& and || are reported individually,
as are operands that are never evaluated because another operand
short-circuits them.
For switch statements, it detects conditions that are provably constant
as well as case and default labels that are provably never selected.
A constant switch condition is reported at the condition with its value
in the message, specialized by the kind of the condition like the
if-condition messages. Never-selected labels are reported individually
at the label, together with the provable values of the condition; they
are also reported when the condition is not constant but its provable
value range excludes the label, and with the dedicated
_type_limits messages when the label is unmatchable due to
the limited range of the condition's data type (for example
case 300 on an unsigned char condition).
A never-selected label is reported even when the code beneath it is
still executed via fall-through from a preceding case; only the label
itself is redundant then.
Dispatch-free switches, whose case labels all label the same statement
as the default label — such as the dangling-else blocker idiom
switch (0) case 0: default: of common testing
frameworks — are not reported.
Projects that deliberately keep defensive default labels
can disable the default_never_selected and
default_never_selected_type_limits messages in the rule
configuration.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
case_never_selected |
Switch case is never selected{switch_cond_values} |
None |
False |
case_never_selected_type_limits |
Switch case is never selected due to limited range of data type{switch_cond_values} |
None |
False |
dead_call_false_branch |
Function call condition is always true{dead_branch_type} |
None |
False |
dead_call_false_branch_type_limits |
Function call condition is always true due to limited range of data type{dead_branch_type} |
None |
False |
dead_call_true_branch |
Function call condition is always false |
None |
False |
dead_call_true_branch_type_limits |
Function call condition is always false due to limited range of data type{dead_branch_type} |
None |
False |
dead_false_branch |
Condition is always true{dead_branch_type} |
None |
False |
dead_false_branch_type_limits |
Condition is always true due to limited range of data type{dead_branch_type} |
None |
False |
dead_false_branch_type_limits_in_context |
Condition is true in context due to limited range of data type{dead_branch_type} |
None |
False |
dead_global_var_false_branch |
Global variable condition is always true{dead_branch_type} |
None |
False |
dead_global_var_false_branch_type_limits |
Global variable condition is always true due to limited range of data type{dead_branch_type} |
None |
False |
dead_global_var_true_branch |
Global variable condition is always false{dead_branch_type} |
None |
False |
dead_global_var_true_branch_type_limits |
Global variable condition is always false due to limited range of data type{dead_branch_type} |
None |
False |
dead_param_dependent_var_false_branch |
Parameter-dependent variable condition is always true{dead_branch_type} |
None |
False |
dead_param_dependent_var_false_branch_type_limits |
Parameter-dependent variable condition is always true due to limited range of data type{dead_branch_type} |
None |
False |
dead_param_dependent_var_true_branch |
Parameter-dependent variable condition is always false{dead_branch_type} |
None |
False |
dead_param_dependent_var_true_branch_type_limits |
Parameter-dependent variable condition is always false due to limited range of data type{dead_branch_type} |
None |
False |
dead_param_false_branch |
Parameter condition is always true{dead_branch_type} |
None |
False |
dead_param_false_branch_type_limits |
Parameter condition is always true due to limited range of data type{dead_branch_type} |
None |
False |
dead_param_null_false_branch |
Parameter is always equal to NULL{dead_branch_type} |
None |
False |
dead_param_null_true_branch |
Parameter is never equal to NULL{dead_branch_type} |
None |
False |
dead_param_true_branch |
Parameter condition is always false{dead_branch_type} |
None |
False |
dead_param_true_branch_type_limits |
Parameter condition is always false due to limited range of data type{dead_branch_type} |
None |
False |
dead_true_branch |
Condition is always false{dead_branch_type} |
None |
False |
dead_true_branch_type_limits |
Condition is always false due to limited range of data type{dead_branch_type} |
None |
False |
dead_true_branch_type_limits_in_context |
Condition is false in context due to limited range of data type{dead_branch_type} |
None |
False |
dead_var_false_branch |
Variable condition is always true{dead_branch_type} |
None |
False |
dead_var_false_branch_type_limits |
Variable condition is always true due to limited range of data type{dead_branch_type} |
None |
False |
dead_var_true_branch |
Variable condition is always false{dead_branch_type} |
None |
False |
dead_var_true_branch_type_limits |
Variable condition is always false due to limited range of data type{dead_branch_type} |
None |
False |
default_never_selected |
Default case is never selected{switch_cond_values} |
None |
False |
default_never_selected_type_limits |
Default case is never selected due to limited range of data type{switch_cond_values} |
None |
False |
do_while_only_once |
Loop is only executed once, loop condition is always false |
None |
False |
do_while_only_once_type_limits |
Loop is only executed once, loop condition is always false due to limited range of data type |
None |
False |
loop_cond_false |
Loop body is dead, condition is always false |
None |
False |
loop_cond_false_type_limits |
Loop body is dead, condition is always false due to limited range of data type |
None |
False |
loop_cond_true |
Loop condition is always true |
None |
False |
loop_cond_true_type_limits |
Loop condition is always true due to limited range of data type |
None |
False |
subexpression_false |
Subexpression always evaluates to false |
None |
False |
subexpression_false_type_limits |
Subexpression always evaluates to false due to limited range of data type |
None |
False |
subexpression_true |
Subexpression always evaluates to true |
None |
False |
subexpression_true_type_limits |
Subexpression always evaluates to true due to limited range of data type |
None |
False |
switch_cond_constant |
Switch condition is always {value} |
None |
False |
switch_cond_constant_call |
Function call switch condition is always {value} |
None |
False |
switch_cond_constant_global_var |
Global variable switch condition is always {value} |
None |
False |
switch_cond_constant_param |
Parameter switch condition is always {value} |
None |
False |
switch_cond_constant_param_dependent_var |
Parameter-dependent variable switch condition is always {value} |
None |
False |
switch_cond_constant_var |
Variable switch condition is always {value} |
None |
False |
unreachable_short_circuit |
Subexpression never evaluated due to short-circuiting operator |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
ignore_literal_subexpressions¶
ignore_literal_subexpressions : bool = True
assert(false && "message"), are exempt from
subexpression_true/subexpression_false findings.
Literal operands outside conditions (for example in assigned values) are
reported regardless, as the subexpression finding is the only report
there.
ignore_short_circuited_constants¶
ignore_short_circuited_constants : bool = True
assert(false && "message"), are exempt
from unreachable_short_circuit findings.
report_do_while_false¶
report_do_while_false : bool = True
do ... while(0) should be reported.
report_for_true¶
report_for_true : bool = True
for(;;) ... should be reported.
report_while_true¶
report_while_true : bool = True
while(1) ... should be reported.