oxc-project/oxc · warning · OxcDiagnostic
Passing `length` as the end argument of a `slice` call is un
Error message
Passing `length` as the end argument of a `slice` call is unnecessary.
What it means
Diagnostic from the oxlint rule `unicorn/no-length-as-slice-end`. For `slice(start, end)`, an end at or beyond the receiver's length behaves exactly like omitting the end argument. Passing the receiver's own `.length` (the rule proves the index expression is identical to the call receiver) is redundant noise; the fix is to delete the second argument. `otherArray.length` is not flagged because it may differ.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_length_as_slice_end.rs:14
use oxc_ast::{
AstKind,
ast::{Argument, Expression, MemberExpression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use crate::{
AstNode, ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_expression,
};
fn no_length_as_slice_end_diagnostic(call_span: Span, arg_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Passing `length` as the end argument of a `slice` call is unnecessary.")
.with_help("Remove the second argument.")
.with_labels([
call_span.label("`.slice` called here."),
arg_span.label("Invalid argument here"),
])
}
#[derive(Debug, Default, Clone)]
pub struct NoLengthAsSliceEnd;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow using `length` as the end argument of a `slice` call.
///
/// ### Why is this bad?
///
/// Passing `length` as the end argument of a `slice` call is unnecessary and can be confusing.View on GitHub (pinned to e1e7af627c)
Solutions
- Remove the second argument: `const copy = items.slice(0)`
- When copying is the only intent, use `const copy = [...items]` (deep copies need structuredClone)
- Suppress inline only in the rare case where the matched length is coincidental and documented
Example fix
// before const copy = items.slice(0, items.length); // after const copy = items.slice(0);
Defensive patterns
Strategy: validation
Prevention
- `slice(start)` already runs to the end — omit the second argument
- Use [...arr] for shallow copies and structuredClone for deep copies
- Treat `slice(0, x.length)` in review as a sign of uncertainty about slice semantics
When it happens
Trigger: `items.slice(0, items.length)`, `text.slice(0, text.length)`, `state.rows.slice(from, state.rows.length)` — the second argument is a static `.length` member on an expression identical to the slice receiver. A `.length` of a different object, or `slice(0, n)`, passes.
Common situations: Defensive copies written by developers unsure whether slice needs an end; ported Java-style `substring(0, n)` habits; generated or template-produced code.
Related errors
- Type can be trivially inferred from the initializer
- Avoid calls to the `Array` constructor
- Use Array destructuring.
- Assign array to a variable before exporting as module defaul
- Prefer `{} {}` over `{} {}` to check {}.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/26ba11daf2d443f7.
Report an issue: GitHub.