A statement in a switch comes before its first label and never runs

tx0/c:code-before-first-casestrictwarningreliability/control-flowdepth fastbug, cwe-561

What it finds

switch (op) { log_op(op); case ADD: ... }: a switch jumps straight to a label, so statements before the first case or default never run, and initializers of declarations there are never applied.

Statements count, and declarations with an initializer other than static ones, whose initializer applies before the program starts; declarations without one are a legitimate way to declare variables for all cases. A statement that calls something other than a function of the C library or of the file may be a macro that expands to labels, and is left alone.

From the rule's source, code_before_first_case.rs.

Example from the rule's tests
int apply(int op, int a){  switch (op) {    a++;tx0 reports: this statement comes before the first label  case 1:    return a;  default:    return 0;  }} int setup(int op){  switch (op) {    int value = 4;tx0 reports: its initializer never runs  case 1:    return 1;  }  switch (op) {    int plain;  case 2:    plain = 2;    return plain;  }  return 0;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:code-before-first-case" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/c:code-before-first-case] reason="..."

tx0 explain tx0/c:code-before-first-case shows the same in the terminal. Found a false positive? Report it.