rust-lang/rust-analyzer · error
Cannot alias reference to `_`
Error message
Cannot alias reference to `_`
What it means
The alias-rename fallback in rename() converts a `use` path segment into `as <new_name>`. Because `_` is not a valid alias identifier in this position, IdentifierKind::Underscore is rejected with this error before any edits are produced.
Source
Thrown at crates/ide/src/rename.rs:173
return rename_elided_lifetime(position, lifetime_token, &new_name_str);
}
let defs = find_definitions(&sema, syntax, position, &new_name)?;
let alias_fallback =
alias_fallback(syntax, position, &new_name.display(db, edition).to_string());
let ops: RenameResult<Vec<SourceChange>> = match alias_fallback {
Some(_) => ok_if_any(
defs
// FIXME: This can use the `ide_db::rename_reference` (or def.rename) method once we can
// properly find "direct" usages/references.
.map(|(.., def, new_name, _)| {
match kind {
IdentifierKind::Ident => (),
IdentifierKind::Lifetime => {
bail!("Cannot alias reference to a lifetime identifier")
}
IdentifierKind::Underscore => bail!("Cannot alias reference to `_`"),
IdentifierKind::LowercaseSelf => {
bail!("Cannot rename alias reference to `self`")
}
};
let mut usages = def.usages(&sema).all();
// FIXME: hack - removes the usage that triggered this rename operation.
match usages.references.get_mut(&file_id).and_then(|refs| {
refs.iter()
.position(|ref_| ref_.range.contains_inclusive(position.offset))
.map(|idx| refs.remove(idx))
}) {
Some(_) => (),
None => never!(),
};
let mut source_change = SourceChange::default();
source_change.extend(usages.references.get_mut(&file_id).iter().map(|refs| {View on GitHub (pinned to e8f7e90aa3)
Solutions
- Use a valid identifier for the alias, e.g. rename to `_unused` style names instead of a bare `_`.
- If the goal is to silence an unused import, delete or prefix the import manually rather than renaming through the alias path.
Example fix
// before rename(use_tree_pos, "_")?; // after rename(use_tree_pos, "_unused_bar")?; // use foo::bar as _unused_bar;
Defensive patterns
Strategy: validation
Validate before calling
// Reject bare underscore as a new alias name
if new_name == "_" {
return Err("alias cannot be `_`");
} Type guard
fn is_valid_alias(name: &str) -> bool {
name != "_" && !name.starts_with('\'')
&& name.chars().all(|c| c.is_alphanumeric() || c == '_')
} Try / catch
match rename(db, pos, new_name, &config) {
Ok(change) => apply(change),
Err(e) if e.to_string().contains("Cannot alias reference to `_`") => {
prompt_user("Choose a non-underscore alias name");
}
Err(e) => report(e),
} Prevention
- Forbid `_` as rename target in editor input validation
- Use underscore-prefixed identifiers like `_unused` if silencing warnings is the goal
- Validate identifier kind before invoking the alias-rename path
When it happens
Trigger: Calling rename with position on a `use` tree path segment and new_name == "_" (IdentifierKind::Underscore classification).
Common situations: Tools or users attempting to 'anonymize' an import via rename; scripted refactors that pass underscore as a placeholder new name.
Related errors
- Cannot alias reference to a lifetime identifier
- Cannot rename alias reference to `self`
- Renaming aliases is currently unsupported
- No file available to rename
- Invalid name `{}`: cannot rename to a keyword
AI-assisted analysis of rust-lang/rust-analyzer@e8f7e90aa3 (2026-09-03).
Data as JSON: /api/errors/7e8570b9bde6df02.
Report an issue: GitHub.