{"record":{"id":"df9cac5159397b13","repo":"oxc-project/oxc","slug":"prefer-includes-over-indexof-when-checking","errorCode":null,"errorMessage":"Prefer `includes()` over `indexOf()` when checking for existence or non-existence.","messagePattern":"Prefer `includes\\(\\)` over `indexOf\\(\\)` when checking for existence or non-existence\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_includes.rs","lineNumber":18,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{ChainElement, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::{BinaryOperator, UnaryOperator};\n\nuse crate::{\n    AstNode,\n    ast_util::{call_expr_method_callee_info, is_method_call},\n    context::LintContext,\n    rule::Rule,\n};\n\nfn prefer_includes_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Prefer `includes()` over `indexOf()` when checking for existence or non-existence.\",\n    )\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferIncludes;\n\n#[derive(Debug, Clone, Copy)]\nenum ComparisonKind {\n    // `indexOf(...) != -1` / `!== -1`\n    ExistsOrUndefined,\n    // `indexOf(...) > -1` / `>= 0`\n    ExistsOnly,\n    // `indexOf(...) == -1` / `=== -1` / `< 0`\n    NotExistsOnly,\n}\n","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_includes.rs#L1-L36","documentation":"Diagnostic from the unicorn/prefer-includes lint rule. It fires when `indexOf()` (or `lastIndexOf()`) is compared against -1 or 0 in a boolean context (e.g. `arr.indexOf(x) !== -1`) to test membership; `includes()` expresses the same check directly and is less error-prone. The flagged input is the binary/comparison expression wrapping the `indexOf` call. It is a lint suggestion, not a runtime error.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/prefer_includes.rs:18 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Replace `x.indexOf(v) !== -1` with `x.includes(v)`","Replace `x.indexOf(v) === -1` with `!x.includes(v)`","Keep `indexOf()` only when the actual index position is needed","Apply the rule's auto-fix"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}