Unreachable code should not be used

  • UNREACHABLE_CODE
  • Error
  • Medium
  • cwe

This rule applies when code is unreachable.

Statements after return, throw, break and continue are not executed so that it might not be a programmer's intent.

Noncompliant Code Example

View with compliant examples side by side
function example1() {
    doSomething();
    return;
    doOther(); // UNREACHABLE_CODE alarm
}

function example2() {
    var x = doSomething();
    return x;
    doOther(); // UNREACHABLE_CODE alarm
}

Compliant Code Example

View with noncompliant examples side by side
// In case when 'return' statement is a mistake
function example1() {
    doSomething();
    doOther();
}

// In case when you do not need statements after 'return' statement
function example2() {
    var x = doSomething();
    return x;
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See

  • CWE-561

  • MISRA C:2012, Rule 2.1: A project shall not contain unreachable code

Was this documentation helpful?