GeneralPurpose-InvariantLoopConditionΒΆ
Loop conditions should not be loop-invariant
Required inputs: IR
Update variables used in exit loop condition
This check detects whether a loop exits given the variables being used in conditional statements.
Good code (last statement updates i used in condition)
for (i = 0; i < 10; ++i) {
...
}
Bad code (forgot to update the variable listener)
void dereg_listeners(Entity *entity)
{
Listener* listener = entity->listeners;
while (listener)
{
listener_deregister(listener, entity);
}
}
Good code (update the variable listener in loop body)
void dereg_listeners(Entity *entity)
{
Listener* listener = entity->listeners;
while (listener)
{
listener_deregister(listener, entity);
listener = listener->next;
}
}
Good code (condition global variable i updated by call)
static int i;
void global_conditional() {
while (i) {
call_with_side_effects();
}
}
Bad code (disabled if also disabled updating p)
void disabled_if(int *p) {
int *q = NULL;
while (p) {
if (q && false) {
p = q;
}
}
}Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
loop_condition_invariant |
Loop never modifies variables used in conditions that can leave the loop. |
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
This rule has no individual options.