{"record":{"id":"a7453daf98907a7a","repo":"oxc-project/oxc","slug":"prefer-over-to-check","errorCode":null,"errorMessage":"Prefer `{} {}` over `{} {}` to check {}.","messagePattern":"Prefer `(.+?) (.+?)` over `(.+?) (.+?)` to check (.+?)\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/consistent_existence_index_check.rs","lineNumber":31,"sourceCode":"pub struct ConsistentExistenceIndexCheck;\n\nfn consistent_existence_index_check_diagnostic(\n    replacement: &GetReplacementOutput,\n    span: Span,\n) -> OxcDiagnostic {\n    let existence_or_non_existence =\n        if replacement.replacement_value == \"-1\" { \"non-existence\" } else { \"existence\" };\n\n    let label = format!(\n        \"Prefer `{} {}` over `{} {}` to check {}.\",\n        replacement.replacement_operator,\n        replacement.replacement_value,\n        replacement.original_operator,\n        replacement.original_value,\n        existence_or_non_existence,\n    );\n\n    OxcDiagnostic::warn(label).with_label(span)\n}\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforce consistent style for element existence checks with `indexOf()`,\n    /// `lastIndexOf()`, `findIndex()`, and `findLastIndex()`. This ensures\n    /// that comparisons are performed in a standard and clear way.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// This rule is meant to enforce a specific style and improve code clarity.\n    /// Using inconsistent comparison styles (e.g., `index < 0`, `index >= 0`)\n    /// can make the intention behind the code unclear, especially in large\n    /// codebases.\n    ///\n    /// ### Examples\n    ///","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/consistent_existence_index_check.rs#L13-L49","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Rewrite the comparison to `=== -1` (non-existence) or `!== -1` (existence).","Run `oxlint --fix` to convert all occurrences automatically.","Where only existence matters, prefer `array.includes(x)` or `array.some(...)` instead of index checks."],"exampleFix":"// before\nconst index = foo.indexOf('bar');\nif (index < 0) {}\n// after\nconst index = foo.indexOf('bar');\nif (index === -1) {}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compare index results only with `=== -1` or `!== -1`.","Prefer `Array.prototype.includes` or `.some()` when you need existence, not the position.","Run `oxlint --fix` once after enabling the rule to normalize the whole codebase."],"tags":["oxlint","lint","unicorn","arrays","style","comparison"],"backgroundTag":"indexof-existence-check-style","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}