rust-lang/rust-analyzer · error
Cannot rename reference to `_` as it is being referenced mul
Error message
Cannot rename reference to `_` as it is being referenced multiple times
What it means
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.
Source
Thrown at crates/ide/src/rename.rs:783
}
let fn_def = match local.parent(sema.db) {
hir::ExpressionStoreOwner::Body(hir::DefWithBody::Function(func)) => func,
_ => bail!("Cannot rename local to self outside of function"),
};
let InFile { file_id, value: self_param } =
sema.source(self_param).ok_or_else(|| format_err!("cannot find function source"))?;
let def = Definition::Local(local);
let usages = def.usages(sema).all();
let edit = text_edit_from_self_param(
&self_param,
new_name.display(sema.db, file_id.edition(sema.db)).to_string(),
)
.ok_or_else(|| format_err!("No target type found"))?;
if usages.len() > 1 && identifier_kind == IdentifierKind::Underscore {
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
}
let mut source_change = SourceChange::default();
source_change.insert_source_edit(file_id.original_file(sema.db).file_id(sema.db), edit);
source_change.extend(usages.iter().map(|(file_id, references)| {
(
file_id.file_id(sema.db),
source_edit_from_references(
sema.db,
references,
def,
new_name,
file_id.edition(sema.db),
),
)
}));
transform_method_call_into_assoc_fn(sema, &mut source_change, fn_def, find_path_config);
Ok(source_change)
}View on GitHub (pinned to e8f7e90aa3)
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 `_`.
Example fix
// before
fn f(self) { self.x(); }
// after
fn f(&self) { self.x(); }
// or if unused:
fn f() {} Defensive patterns
Strategy: validation
Validate before calling
// guard before renaming to "_": count usages
if identifier_kind == IdentifierKind::Underscore && usage_count > 1 {
// pick a named identifier or fix usages first
} Prevention
- 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.
When it happens
Trigger: Renaming `self` (or a reference to it) to `_` while `usages.len() > 1`, i.e. the identifier is referenced more than once.
Common situations: Trying to silence an unused `self` by renaming it to `_` when the method body still uses `self` in several places.
Related errors
- Cannot rename builtin type
- Cannot rename a builtin attr.
- Cannot rename `Self`
- Invalid name `{0}`: cannot rename module to {0}
- Invalid name `{}`: not a lifetime identifier
AI-assisted analysis of rust-lang/rust-analyzer@e8f7e90aa3 (2026-09-03).
Data as JSON: /api/errors/6f787ad201d73457.
Report an issue: GitHub.