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

  1. Remove the second argument: `const copy = items.slice(0)`
  2. When copying is the only intent, use `const copy = [...items]` (deep copies need structuredClone)
  3. 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

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


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/26ba11daf2d443f7. Report an issue: GitHub.