switch and its cases should be compared with equal types

  • SWITCH_CASE_INCOMPATIBLE_TYPE
  • Error
  • Medium
  • No tags

This rule applies when switch and its cases are compared with different types. That is, switch statement has numeric string value and case statements have number value.

Because switch and its cases are compared with strict equality, cases with different types do not match. A programmer might incorrectly think numeric string and number match by implicit type conversion, but it's not the case.

Noncompliant Code Example

View with compliant examples side by side
var s = '42';
switch (s) { // SWITCH_CASE_INCOMPATIBLE_TYPE alarm
case 0:
case 42:
    matched = true;
    break;
}

Compliant Code Example

View with noncompliant examples side by side
var s = 42;
switch (s) {
case 0:
case 42:
    matched = true;
    break;
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See

Was this documentation helpful?