{"record":{"id":"99b1a0a29035fd63","repo":"oxc-project/oxc","slug":"prefer-indexof-over-findindex-for-simple-equal","errorCode":null,"errorMessage":"Prefer `indexOf` over `findIndex` for simple equality checks","messagePattern":"Prefer `indexOf` over `findIndex` for simple equality checks","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_array_index_of.rs","lineNumber":13,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Expression, FormalParameter, Statement},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\nuse oxc_syntax::operator::BinaryOperator;\n\nuse crate::{AstNode, ast_util::is_method_call, context::LintContext, rule::Rule};\n\nfn prefer_array_index_of_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `indexOf` over `findIndex` for simple equality checks\")\n        .with_help(\"Use `indexOf(value)` instead of `findIndex(x => x === value)` for better clarity and performance\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferArrayIndexOf;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforces using `indexOf` or `lastIndexOf` instead of `findIndex` or `findLastIndex`\n    /// when the callback is a simple strict equality comparison.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Using `findIndex(x => x === value)` is unnecessarily verbose when `indexOf(value)`\n    /// accomplishes the same thing more concisely and clearly. It also avoids the overhead\n    /// of creating a callback function.","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_array_index_of.rs#L1-L31","documentation":"Lint diagnostic from oxlint's `unicorn/prefer-array-index-of` rule. When the `findIndex`/`findLastIndex` callback only does a strict equality check against a value (`x => x === value`), it is exactly what `indexOf`/`lastIndexOf` already do, with less ceremony and typically faster engine-optimized code. The rule flags those simple callbacks and tells you to use `indexOf`.","triggerScenarios":"`array.findIndex(x => x === target)`, `array.findIndex(x => target === x)`, `array.findLastIndex(x => x === target)` — callbacks whose body is a single strict-equality (`===`) comparison using only the first parameter. Reported during oxlint runs on such CallExpressions.","commonSituations":"Developers who learned `findIndex` first and use it uniformly, or code migrated from `filter(x => x === v)[0]`-style rewrites. Also common in id-lookup code (`items.findIndex(i => i === selectedId)`). Appears when the unicorn category is enabled.","solutions":["Replace with `array.indexOf(target)` (or `lastIndexOf` for `findLastIndex`).","Run `oxlint --fix` to rewrite matches automatically.","Keep `findIndex` when the comparison is not strict equality or involves multiple values/properties; suppress inline if you intentionally keep it (e.g. for future predicate growth).","Disable the rule if the team standard is to always use `findIndex`/`findLastIndex` for symmetry."],"exampleFix":"// before\nconst idx = ids.findIndex(id => id === selected);\n\n// after\nconst idx = ids.indexOf(selected);","handlingStrategy":"validation","validationCode":"// oxlint --fix --filter unicorn/prefer-array-index-of src/\n// CI gate: oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use `indexOf` for strict-equality value lookup; keep `findIndex` only for real predicates.","During code review, flag `findIndex(x => x === v)` as an automatic rewrite."],"tags":["oxlint","unicorn","arrays","performance","refactor","autofix"],"backgroundTag":"lint-rule-violation","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}