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

  • 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

Compliant Code Example

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


This rule was introduced in DeepScan 1.31.0.


Was this documentation helpful?