{"record":{"id":"fa90a52a673f8e1f","repo":"oxc-project/oxc","slug":"prefer-negative-index-over-length-index-when","errorCode":null,"errorMessage":"Prefer negative index over `.length - index` when possible.","messagePattern":"Prefer negative index over `\\.length - index` when possible\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_negative_index.rs","lineNumber":15,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{\n        Argument, ArrayExpressionElement, BinaryExpression, BinaryOperator, Expression,\n        StaticMemberExpression,\n    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, context::LintContext, fixer::Fix, rule::Rule, utils::is_same_expression};\n\nfn prefer_negative_index_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer negative index over `.length - index` when possible.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferNegativeIndex;\n\n#[derive(Debug)]\nenum TypeOptions {\n    String,\n    Array,\n    TypedArray,\n    Literal,\n    Unknown,\n}\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_negative_index.rs#L1-L33","documentation":"Diagnostic from the unicorn/prefer-negative-index lint rule. It fires when an expression of the form `array.length - n` is used as an index (in member access, `at()`, `splice`, `slice`, or method arguments) and `n` is a positive number or the same array variable; a negative index (`array.at(-1)`, `arr[arr.length - 1]` -> `arr.at(-1)`) is the idiomatic, shorter form supported by modern methods. It is a lint suggestion, not a runtime error.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/prefer_negative_index.rs:15 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Replace `arr[arr.length - 1]` with `arr.at(-1)`","Replace `arr.length - n` arguments to `at`/`slice`/`splice` with `-n`","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-14T05:17:10.506Z"}