{"record":{"id":"a1d2c095b2c276a2","repo":"oxc-project/oxc","slug":"expected-a-for-of-loop-instead-of-a-for-loop","errorCode":null,"errorMessage":"Expected a `for...of` loop instead of a `for` loop with this simple iteration.","messagePattern":"Expected a `for\\.\\.\\.of` loop instead of a `for` loop with this simple iteration\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/typescript/prefer_for_of.rs","lineNumber":16,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{\n        AssignmentTarget, BindingPattern, Expression, ForStatementInit, SimpleAssignmentTarget,\n        VariableDeclarationKind, match_member_expression,\n    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator, UpdateOperator};\n\nuse crate::{AstNode, context::LintContext, rule::Rule, utils::is_same_expression};\n\nfn prefer_for_of_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Expected a `for...of` loop instead of a `for` loop with this simple iteration.\",\n    )\n    .with_help(\"Consider using a `for...of` loop for this simple iteration.\")\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferForOf;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforces the use of a `for...of` loop instead of a `for` loop with simple iteration.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Using a `for` loop with a simple iteration over an array can be replaced with a more concise\n    /// and readable `for...of` loop. `for...of` loops are easier to read and less error-prone, as they","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/typescript/prefer_for_of.rs#L1-L34","documentation":"Raised by typescript/prefer_for_of when a `for` loop is a simple index iteration over an array (counter starts at 0, condition is index < arr.length, increment by one, and the body only uses `arr[i]` without reassigning the index or mutating the array's length). At the throw site the loop can be expressed more safely and readably as `for (const item of arr)`, eliminating off-by-one hazards. prefer_for_of_diagnostic fires from run() after the structural checks confirm the pattern is a plain iteration.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/typescript/prefer_for_of.rs:16 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Rewrite the loop as for (const item of array) { ... }.","If array methods fit, use forEach/map instead."],"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"}