{"record":{"id":"034b561a8b04bb01","repo":"oxc-project/oxc","slug":"do-not-use-array-foreach","errorCode":null,"errorMessage":"Do not use `Array#forEach`","messagePattern":"Do not use `Array#forEach`","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/no_array_for_each.rs","lineNumber":33,"sourceCode":"};\n\nfn no_array_for_each_diagnostic(\n    span: Span,\n    callback_arguments: CallbackArguments,\n) -> OxcDiagnostic {\n    let help = match callback_arguments {\n        CallbackArguments::ElementOnly => {\n            \"Replace it with a `for…of` loop. It is faster, more readable, and allows early exits with `break` or `return`.\"\n        }\n        CallbackArguments::Index => {\n            \"For arrays that need the index, replace it with a `for…of` loop over `.entries()`, such as `for (const [index, element] of array.entries())`. Otherwise, use the appropriate `for…of` loop. Array `.entries()` keeps indexes numeric and loops allow early exits with `break` or `return`.\"\n        }\n        CallbackArguments::Extra => {\n            \"Replace it with a `for…of` loop that preserves the extra callback arguments you use. For arrays, `.entries()` provides the numeric index, and the original array can be referenced directly if needed.\"\n        }\n    };\n\n    OxcDiagnostic::warn(\"Do not use `Array#forEach`\").with_help(help).with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoArrayForEach;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Forbids the use of `Array#forEach` in favor of a for loop.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Benefits of [`for…of` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) over the `forEach` method can include:\n    ///\n    /// - Faster\n    /// - Better readability\n    /// - Ability to exit early with `break` or `return`\n    ///","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_array_for_each.rs#L15-L51","documentation":"Raised by unicorn/no-array-for-each when `Array#forEach` is used. The rule inspects the callback's parameter shape (element-only vs index-using) during run() and selects tailored advice; the faulting input is the `.forEach(...)` call at the labeled span.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/no_array_for_each.rs:33 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Replace with a `for…of` loop, which is faster and supports `break`/`return` early exits","When the index is needed, iterate `for (const [index, element] of array.entries())`","For pure transformations, prefer `map`, `filter`, or `reduce` to express intent"],"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-14T00:17:10.932Z"}