{"record":{"id":"8fe6cb0be1516681","repo":"oxc-project/oxc","slug":"name-is-never-reassigned","errorCode":null,"errorMessage":"`{name}` is never reassigned.","messagePattern":"`(.+?)` is never reassigned\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/prefer_const.rs","lineNumber":20,"sourceCode":"    AssignmentTarget, AssignmentTargetMaybeDefault, AssignmentTargetProperty, VariableDeclaration,\n};\nuse oxc_ast::{AstKind, ast::VariableDeclarationKind};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::SymbolId;\nuse oxc_span::{GetSpan, Span};\nuse schemars::JsonSchema;\nuse serde::{Deserialize, Serialize};\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    fixer::{RuleFix, RuleFixer},\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn prefer_const_diagnostic(name: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"`{name}` is never reassigned.\"))\n        .with_help(\"Use `const` instead.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\nstruct PreferConstConfig {\n    /// Configures how destructuring assignments are handled.\n    destructuring: Destructuring,\n    /// If `true`, the rule will not report variables that are read before their initial assignment.\n    /// This is mainly useful for preventing conflicts with the `typescript/no-use-before-define` rule.\n    ignore_read_before_assign: bool,\n}\n\n#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\")]\nenum Destructuring {\n    /// Warn if any of the variables in a destructuring assignment should be `const`.","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/prefer_const.rs#L2-L38","documentation":"Diagnostic from the oxlint `prefer-const` rule. It fires for a `let` (or `var`) binding that is assigned exactly once at declaration and never reassigned; the message names the binding and suggests `const` (prefer_const.rs:20-24). Destructuring behavior is controlled by the `destructuring` option and read-before-assign cases by `ignoreReadBeforeAssign` (shown in the config struct in the same file).","triggerScenarios":"Enable `prefer-const` (part of the correctness-ish default presets in most configs) and write `let x = 1;` where `x` is never reassigned. Also fires for `for (let i of arr)` when `i` is not reassigned inside the loop, and in destructuring per the `destructuring: \"any\" | \"all\"` option.","commonSituations":"Developers typing `let` out of habit for every binding; code migrated from older ES5 var-based style; loops and imports fixed to const during cleanup; `let` inside `try` blocks reassigned only on some paths being misjudged (true reassigned cases are correctly skipped).","solutions":["Change `let` to `const` for the flagged binding (auto-fixable with `oxlint --fix`).","If the variable is conditionally reassigned later, restructure so the reassignment is visible to the linter or keep `let` and justify it.","Adjust `destructuring` option (\"any\" vs \"all\") if destructured bindings are reported unexpectedly.","Inline-disable for interop patterns (e.g. reassigning a binding from a hot-reload shim)."],"exampleFix":"// before\nlet answer = 42;\nconsole.log(answer);\n\n// after\nconst answer = 42;\nconsole.log(answer);","handlingStrategy":"validation","validationCode":"// .oxlintrc.json — align destructuring policy with your codebase\n{ \"rules\": { \"prefer-const\": [\"warn\", { \"destructuring\": \"all\", \"ignoreReadBeforeAssign\": false }] } }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Type `const` first; switch to `let` only when a reassignment appears.","Run `oxlint --fix` on a branch when adopting prefer-const in a legacy repo.","Set destructuring to \"all\" to avoid churn in mixed destructure groups."],"tags":["oxlint","eslint","variables","const","best-practice"],"backgroundTag":"eslint-prefer-const","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"}