{"record":{"id":"248027fb7f3e31b8","repo":"oxc-project/oxc","slug":"prefer-some-over-non-zero-length-check-from","errorCode":null,"errorMessage":"Prefer `.some(…)` over non-zero length check from `.filter(…)`.","messagePattern":"Prefer `\\.some\\(…\\)` over non-zero length check from `\\.filter\\(…\\)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs","lineNumber":23,"sourceCode":"use oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::BinaryOperator;\n\nuse crate::{\n    AstNode,\n    ast_util::{call_expr_method_callee_info, is_method_call, outermost_paren_parent},\n    context::LintContext,\n    rule::Rule,\n    utils::is_boolean_node,\n};\n\nfn over_method(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `.some(…)` over `.find(…)` or `.findLast(…)`.\").with_label(span)\n}\n\nfn non_zero_filter(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `.some(…)` over non-zero length check from `.filter(…)`.\")\n        .with_label(span)\n}\n\nfn negative_one_or_zero_filter(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `.some(…)` over `.findIndex(…)` or `.findLastIndex(…)`.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferArraySome;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefers using [`Array#some()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) over [`Array#find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find), [`Array#findLast()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast) with comparing to `undefined`,\n    /// or [`Array#findIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex), [`Array#findLastIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex)\n    /// and a non-zero length check on the result of [`Array#filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)\n    ///","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs#L5-L41","documentation":"Lint diagnostic from oxlint's `unicorn/prefer-array-some` rule (variant `non_zero_filter`). `filter(fn).length > 0` (or `!== 0`, `> 0`) builds an entire filtered array just to test whether anything matched. `some(fn)` expresses existence directly, short-circuits at the first match, and allocates nothing. This message fires on the non-zero length check over a `filter` result.","triggerScenarios":"Patterns like `errors.filter(isFatal).length > 0`, `if (list.filter(x => x.active).length !== 0)`, truthiness on `filter(...).length`. Detected as a `filter` call whose `.length` participates in a non-zero comparison (via `call_expr_method_callee_info` and boolean-context analysis in the rule).","commonSituations":"One of the most common anti-patterns in review feedback; appears when teams enable the unicorn category or migrate from ESLint unicorn. Especially costly on hot paths with large arrays, since filter always runs to completion.","solutions":["Replace `arr.filter(fn).length > 0` with `arr.some(fn)` (and `.length === 0` with `.every(x => !fn(x))` or `!arr.some(fn)`).","Run `oxlint --fix` to convert matched patterns automatically.","Keep `filter` when the filtered array is used afterwards; if only sometimes, hoist `const matches = arr.filter(fn);` and check `matches.length` — suppress the line if still flagged.","Disable the rule if the codebase intentionally prefers filter-length chains."],"exampleFix":"// before\nconst hasFatal = errors.filter(e => e.fatal).length > 0;\n\n// after\nconst hasFatal = errors.some(e => e.fatal);","handlingStrategy":"validation","validationCode":"// oxlint --fix --filter unicorn/prefer-array-some src/\n// CI gate: oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Write `arr.some(fn)` instead of `arr.filter(fn).length > 0` by habit — it short-circuits and allocates nothing.","In review, treat filter-length-in-boolean-context as a blocking nit; it is both slower and less readable."],"tags":["oxlint","unicorn","arrays","performance","boolean-logic","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-14T05:17:10.506Z"}