Comparison expressions should not be placed as indexOf() argument itself

  • MISPLACED_COMPARISON_AT_INDEX_OF
  • Error
  • Medium
  • No tags

This rule applies when a comparison expression is incorrectly placed as an indexOf() argument itself.

For example, str.indexOf('x' > -1) searches the occurrence of the 'false' string, which is unlikely to be intended. This may happen as a result of placing the closing parenthesis in wrong position. The correct code should be str.indexOf('x') > -1.

This rule also applies to the analogous lastIndexOf() comparsion.

Noncompliant Code Example

View with compliant examples side by side
if (str.indexOf(foo > -1)) { // MISPLACED_COMPARISON_AT_INDEX_OF alarm
    doSomething(str);
}

Compliant Code Example

View with noncompliant examples side by side
if (str.indexOf(foo) > -1) {
    doSomething(str);
}

Version

This rule was introduced in DeepScan 1.31.0.

See