{"record":{"id":"6f787ad201d73457","repo":"rust-lang/rust-analyzer","slug":"cannot-rename-reference-to-as-it-is-being-refe-6f787a","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":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide/src/rename.rs","lineNumber":783,"sourceCode":"    }\n\n    let fn_def = match local.parent(sema.db) {\n        hir::ExpressionStoreOwner::Body(hir::DefWithBody::Function(func)) => func,\n        _ => bail!(\"Cannot rename local to self outside of function\"),\n    };\n\n    let InFile { file_id, value: self_param } =\n        sema.source(self_param).ok_or_else(|| format_err!(\"cannot find function source\"))?;\n\n    let def = Definition::Local(local);\n    let usages = def.usages(sema).all();\n    let edit = text_edit_from_self_param(\n        &self_param,\n        new_name.display(sema.db, file_id.edition(sema.db)).to_string(),\n    )\n    .ok_or_else(|| format_err!(\"No target type found\"))?;\n    if usages.len() > 1 && identifier_kind == IdentifierKind::Underscore {\n        bail!(\"Cannot rename reference to `_` as it is being referenced multiple times\");\n    }\n    let mut source_change = SourceChange::default();\n    source_change.insert_source_edit(file_id.original_file(sema.db).file_id(sema.db), edit);\n    source_change.extend(usages.iter().map(|(file_id, references)| {\n        (\n            file_id.file_id(sema.db),\n            source_edit_from_references(\n                sema.db,\n                references,\n                def,\n                new_name,\n                file_id.edition(sema.db),\n            ),\n        )\n    }));\n    transform_method_call_into_assoc_fn(sema, &mut source_change, fn_def, find_path_config);\n    Ok(source_change)\n}","sourceCodeStart":765,"sourceCodeEnd":801,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide/src/rename.rs#L765-L801","documentation":"When renaming a `self` parameter to `_`, rust-analyzer allows the edit only if the identifier is used at most once; `usages.len() > 1` with `IdentifierKind::Underscore` would silently break all but one use, so it bails. `_` is not a bindable name, so multiple references would no longer compile.","triggerScenarios":"Renaming `self` (or a reference to it) to `_` while `usages.len() > 1`, i.e. the identifier is referenced more than once.","commonSituations":"Trying to silence an unused `self` by renaming it to `_` when the method body still uses `self` in several places.","solutions":["Remove or fix all other uses of `self` in the body before renaming to `_`.","Use a named identifier (e.g. `this`) instead of `_` if multiple references remain.","If `self` is genuinely unused, delete it from the parameter list rather than renaming to `_`."],"exampleFix":"// before\nfn f(self) { self.x(); }\n// after\nfn f(&self) { self.x(); }\n// or if unused:\nfn f() {}","handlingStrategy":"validation","validationCode":"// guard before renaming to \"_\": count usages\nif identifier_kind == IdentifierKind::Underscore && usage_count > 1 {\n    // pick a named identifier or fix usages first\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Rename to `_` only when the identifier is used at most once.","Prefer deleting an unused `self` parameter outright.","Use a real name (e.g. `this`) when references must survive the rename."],"tags":["rust","rename","underscore","unused-variable"],"backgroundTag":"rename-to-underscore-multiple-usages","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"}