scitools.com

← Metric catalog

Unreachable Statements is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.

Unreachable Statements

API ID: CountUnreachableStmt
Languages: Any
Targets: Functions

The number of statements a function can never reach

A statement is unreachable when no path through the function's control flow graph arrives at it. Structural nodes such as else and end if are not counted, so the value is a count of statements rather than of control flow nodes.

Values inside conditions are not evaluated, so a branch guarded by a condition that is always false still counts as reachable. Only code excluded by control flow — such as code after a return or an unlabeled goto target — is counted.

A statement placed before the first case label of a switch is not counted, because the control flow graph reaches it directly from the switch.

Example:

    int afterReturn(int x)      // CountUnreachableStmt = 1
    {
      return x;
      x = 5;                    // +1 nothing reaches this
    }

    int afterGoto(int x)        // CountUnreachableStmt = 2
    {
      goto label;
      x = 1;                    // +1
      x = 2;                    // +1
    label:
      return x;
    }

    int reachable(int x)        // CountUnreachableStmt = 0
    {
      if (x > 0)
        return 1;
      return 0;
    }
    

See also the Unreachable Code check, which reports the first unreachable statement in each function as a violation.