{"record":{"id":"9e9f5418212371e9","repo":"rust-lang/rust-analyzer","slug":"cannot-rename-reference-to-as-it-is-being-refe","errorCode":null,"errorMessage":"Cannot rename reference to `_` as it is being referenced multiple times","messagePattern":"Cannot rename reference to `_` as it is being referenced multiple times","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide-db/src/rename.rs","lineNumber":400,"sourceCode":"                bail!(\"Invalid name `{}`: not an identifier\", new_name.display(sema.db, edition));\n            }\n            IdentifierKind::Ident => cov_mark::hit!(rename_non_local),\n            IdentifierKind::Underscore => (),\n            IdentifierKind::LowercaseSelf => {\n                bail!(\n                    \"Invalid name `{}`: cannot rename to `self`\",\n                    new_name.display(sema.db, edition)\n                );\n            }\n        }\n    }\n\n    let def = convert_to_def_in_trait(sema.db, def);\n    let usages = def.usages(sema).all();\n\n    if !usages.is_empty() && ident_kind == IdentifierKind::Underscore {\n        cov_mark::hit!(rename_underscore_multiple);\n        bail!(\"Cannot rename reference to `_` as it is being referenced multiple times\");\n    }\n    let mut source_change = SourceChange::default();\n    source_change.extend(usages.iter().map(|(file_id, references)| {\n        let edition = file_id.edition(sema.db);\n        (\n            file_id.file_id(sema.db),\n            source_edit_from_references(sema.db, references, def, &new_name, edition),\n        )\n    }));\n\n    if let Definition::Field(field) = def {\n        rename_field_constructors(sema, field, &new_name, &mut source_change, config);\n    }\n\n    if rename_definition == RenameDefinition::Yes {\n        // This needs to come after the references edits, because we change the annotation of existing edits\n        // if a conflict is detected.\n        let (file_id, edit) =","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide-db/src/rename.rs#L382-L418","documentation":"`rename_reference` refuses a rename to `_` when the definition has existing usages: `_` bindings drop values immediately, so renaming a multiply-referenced binding to `_` would change semantics (each use would move/drop differently). rust_analyzer bails with \"Cannot rename reference to `_` as it is being referenced multiple times\" and marks the path with `cov_mark::hit!(rename_underscore_multiple)`.","triggerScenarios":"Calling rename with new name `\"_\"` (classified `IdentifierKind::Underscore`) on a definition where `def.usages(sema).all()` is non-empty, i.e. the name is referenced at least once elsewhere, at crates/ide-db/src/rename.rs:400.","commonSituations":"User tries to silence an unused-variable warning by renaming a still-used variable to `_`; the IDE blocks it because `_` cannot hold multiple references meaningfully.","solutions":["Prefix the name with an underscore instead (e.g. `_x`) — this silences unused warnings while keeping the binding usable.","Remove the usages first if the variable is genuinely no longer needed, then rename to `_`.","Refactor the code so the value is consumed exactly once if `_` is truly desired."],"exampleFix":"// before\nrename(local_def, \"_\")?; // Err: referenced multiple times\n// after\nrename(local_def, \"_x\")?; // ok: underscore-prefixed keeps the binding","handlingStrategy":"validation","validationCode":"if new_name == \"_\" && !def.usages(sema).all().is_empty() {\n    return Err(anyhow!(\"rename to `_` requires a definition with no other usages; use `_name` instead\"));\n}","typeGuard":"fn can_rename_to_underscore(def: &Definition, sema: &Semantics<'_, RootDatabase>) -> bool {\n    def.usages(sema).all().is_empty()\n}","tryCatchPattern":"match def.rename(sema, \"_\", ...) {\n    Err(e) if e.to_string().contains(\"referenced multiple times\") => suggest_underscore_prefix(),\n    other => other?,\n}","preventionTips":["Prefer underscore-prefixed names (`_x`) to silence unused warnings while keeping the binding.","Only rename to bare `_` for definitions with zero remaining usages.","Check usages with `def.usages(sema)` before proposing `_`."],"tags":["rust","ide","rename","semantics"],"backgroundTag":"rename-to-underscore-not-allowed","analyzedSha":"e8f7e90aa3e7b26aa9a000200f606c1078da99ec","analyzedAt":"2026-09-03T21:08:06.959Z","contentChangedAt":"2026-09-03T21:08:06.959Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}