{"record":{"id":"098699ca8e87e55b","repo":"oxc-project/oxc","slug":"found-a-useless-array-length-check","errorCode":null,"errorMessage":"Found a useless array length check","messagePattern":"Found a useless array length check","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/no_useless_length_check.rs","lineNumber":15,"sourceCode":"use std::fmt::Debug;\n\nuse oxc_ast::{\n    AstKind,\n    ast::{Expression, LogicalExpression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\nuse oxc_syntax::operator::{BinaryOperator, LogicalOperator};\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn some(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Found a useless array length check\")\n        .with_help(\n            \"The non-empty check is useless as `Array#some()` returns `false` for an empty array.\",\n        )\n        .with_label(span)\n}\n\nfn every(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Found a useless array length check\")\n        .with_help(\n            \"The empty check is useless as `Array#every()` returns `true` for an empty array.\",\n        )\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoUselessLengthCheck;\n\ndeclare_oxc_lint!(","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_useless_length_check.rs#L1-L33","documentation":"Diagnostic from the oxlint rule `unicorn/no-useless-length-check` (correctness category). This variant (the `some` diagnostic) fires when a logical `&&` chain contains both a non-empty length check on an array (`array.length !== 0` or `array.length > 0`) and an `array.some(...)` call on the same identifier. `Array#some()` already returns `false` for an empty array, so the non-empty check is redundant.","triggerScenarios":"`array.length > 0 && array.some(Boolean)`, `array.length !== 0 && array.some(Boolean)`, either order (`array.some(Boolean) && array.length > 0`), flattened chains (`foo && array.length !== 0 && bar && array.some(Boolean)`), and parenthesized operands. Requires the same array identifier on both sides, strict operators only (`!==`, `>`; not `!=`, `>=`), the literal `0` on the right, and a non-optional `.some` call (`array?.some(...)` is skipped).","commonSituations":"Guard clauses written defensively around `some`, often copy-pasted from code that used `for` loops or `.filter().length`. Appears when enabling the oxlint correctness category or unicorn plugin.","solutions":["Delete the length check: `if (array.length > 0 && array.some(fn))` -> `if (array.some(fn))`.","If the two operands reference different arrays (`array1` vs `array2`), rename so it is clear they are intentionally different; the rule correctly skips those.","Keep the check only when `.some` is optional-chained, since `array?.some(...)` short-circuits differently.","Add an inline disable comment for deliberate cases."],"exampleFix":"// before\nif (array.length > 0 && array.some(isReady)) {\n  run();\n}\n\n// after\nif (array.some(isReady)) {\n  run();\n}","handlingStrategy":"validation","validationCode":"# detect non-empty length checks paired with .some() in && chains\nrg -n --type js -U '\\.length\\s*(?:!==|>)\\s*0[\\s\\S]{0,80}&&[\\s\\S]{0,80}\\.some\\(' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember `Array#some` returns false on empty arrays; no guard needed.","Write predicates without length pre-checks; add a unit test for the empty case instead.","Enable unicorn/no-useless-length-check in CI to catch regressions."],"tags":["lint","oxlint","unicorn","array","boolean-logic","redundant-check","correctness"],"backgroundTag":"redundant-array-length-check","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"}