Constant expression should not be used in the conditional statements

  • CONSTANT_CONDITION
  • Code Quality
  • Medium, Low
  • cwe

This rule applies when a condition check has always the same result.

For example, it occurs when constant-valued variables are used or the condition is redundant with previously checked one. This rule also applies when only one case is executed due to the result of switch condition check being always the same.

It could be a debugging code, but at worst it is a programmer's mistake that completely misses block of code intended to be executed.

Especially, if a redundant condition makes some code unreachable or conflicts with previously checked one, 'Medium' impact is assigned to the alarm because such code is more likely to have problems.

Note:

  1. Not applied if the variable name is debug or dbg.
  2. Not applied if the variable is declared with a constant value and never assigned again.
  3. Not applied for unnecessary optional chaining on always non-null values because it is usually harmless.

Noncompliant Code Example

View with compliant examples side by side
function example1() {
    if (y) {
        x = 42;
    } else {
        x = 42;
    }
    if (x > 0) { // CONSTANT_CONDITION alarm: Condition 'x > 0' is always satisfied at this point.
        console.log(x);
    }
}

function example2() {
    if (y) {
        x = 42;
    } else {
        x = 42;
    }
    switch (x) {
        case 0: z = 0; break;
        case 42: z = 1; break; // CONSTANT_CONDITION alarm: This switch case is always matched.
    }
}

function example3() {
    if (x) {
        if (x != null) { // CONSTANT_CONDITION alarm: Condition 'x != null' is always satisfied because it is redundant with the above condition 'x'.
            console.log(x.p);
        }
    }
}

function example4() {
    if (x < 0) {
        if (x < 42) { // CONSTANT_CONDITION alarm: Condition 'x < 42' is always satisfied at this point because it is redundant with the above condition 'x < 0'.
            console.log('x is between 0 and 42');
        }
    }
}

function example5(x, y) {
    var arr = [];
    if (x) arr.push(x);
    if (y) arr.push(y);
    if (arr) { // CONSTANT_CONDITION alarm: Condition 'arr' is always satisfied at this point because it is an array. Did you mean 'arr.length' instead?
        console.log(arr.join(", "));
    }
}

Compliant Code Example

View with noncompliant examples side by side
function example1() {
    if (y) {
        x = 42;
    } else {
        x = 0;
    }
    if (x > 0) {
        console.log(x);
    }
}

function example2() {
    if (y) {
        x = 42;
    } else {
        x = 0;
    }
    switch (x) {
        case 0: z = 0; break;
        case 42: z = 1; break;
    }
}

function example3() {
    if (x) {
        console.log(x.p);
    }
}

function example4() {
    if (x > 0) {
        if (x < 42) {
            console.log('x is between 0 and 42');
        }
    }
}

function example5(x, y) {
    var arr = [];
    if (x) arr.push(x);
    if (y) arr.push(y);
    if (arr.length) {
        console.log(arr.join(", "));
    }
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See

  • CWE-489

  • CWE-569

  • CWE-570

  • CWE-571

  • MISRA C:2012, Rule 2.2: There shall be no dead code

  • MISRA C:2012, Rule 14.3: Controlling expressions shall not be invariant

Was this documentation helpful?