oxc-project/oxc · warning · OxcDiagnostic

Prefer `{} {}` over `{} {}` to check {}.

Error message

Prefer `{} {}` over `{} {}` to check {}.

What it means

Diagnostic from oxlint's `unicorn/consistent-existence-index-check` rule. It enforces the `=== -1` / `!== -1` style for existence checks with `indexOf`, `lastIndexOf`, `findIndex`, and `findLastIndex`: the rule resolves a `const` variable initialized to a member call of one of those four methods, then reports comparisons of that identifier using other operators, building the message from a replacement table (e.g. `Prefer \`!== -1\` over \`>= 0\` to check existence.` or `Prefer \`=== -1\` over \`< 0\` to check non-existence.`). An autofix rewrites the operator and value in place.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/consistent_existence_index_check.rs:31

pub struct ConsistentExistenceIndexCheck;

fn consistent_existence_index_check_diagnostic(
    replacement: &GetReplacementOutput,
    span: Span,
) -> OxcDiagnostic {
    let existence_or_non_existence =
        if replacement.replacement_value == "-1" { "non-existence" } else { "existence" };

    let label = format!(
        "Prefer `{} {}` over `{} {}` to check {}.",
        replacement.replacement_operator,
        replacement.replacement_value,
        replacement.original_operator,
        replacement.original_value,
        existence_or_non_existence,
    );

    OxcDiagnostic::warn(label).with_label(span)
}

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Enforce consistent style for element existence checks with `indexOf()`,
    /// `lastIndexOf()`, `findIndex()`, and `findLastIndex()`. This ensures
    /// that comparisons are performed in a standard and clear way.
    ///
    /// ### Why is this bad?
    ///
    /// This rule is meant to enforce a specific style and improve code clarity.
    /// Using inconsistent comparison styles (e.g., `index < 0`, `index >= 0`)
    /// can make the intention behind the code unclear, especially in large
    /// codebases.
    ///
    /// ### Examples
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite the comparison to `=== -1` (non-existence) or `!== -1` (existence).
  2. Run `oxlint --fix` to convert all occurrences automatically.
  3. Where only existence matters, prefer `array.includes(x)` or `array.some(...)` instead of index checks.

Example fix

// before
const index = foo.indexOf('bar');
if (index < 0) {}
// after
const index = foo.indexOf('bar');
if (index === -1) {}
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: `const index = foo.indexOf('bar');` followed by `if (index < 0) {}`, `index >= 0`, `index > -1`, or `index <= -1`. Only fires when the variable is declared `const` and initialized directly to one of the four method calls.

Common situations: Legacy code using `>= 0`/`< 0` pre-`includes()` style; mixed comparison idioms across a large codebase; enabling the unicorn style preset and getting a batch of these at once.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/a7453daf98907a7a. Report an issue: GitHub.