{"record":{"id":"7e8570b9bde6df02","repo":"rust-lang/rust-analyzer","slug":"cannot-alias-reference-to","errorCode":null,"errorMessage":"Cannot alias reference to `_`","messagePattern":"Cannot alias reference to `_`","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide/src/rename.rs","lineNumber":173,"sourceCode":"        return rename_elided_lifetime(position, lifetime_token, &new_name_str);\n    }\n\n    let defs = find_definitions(&sema, syntax, position, &new_name)?;\n    let alias_fallback =\n        alias_fallback(syntax, position, &new_name.display(db, edition).to_string());\n\n    let ops: RenameResult<Vec<SourceChange>> = match alias_fallback {\n        Some(_) => ok_if_any(\n            defs\n                // FIXME: This can use the `ide_db::rename_reference` (or def.rename) method once we can\n                // properly find \"direct\" usages/references.\n                .map(|(.., def, new_name, _)| {\n                    match kind {\n                        IdentifierKind::Ident => (),\n                        IdentifierKind::Lifetime => {\n                            bail!(\"Cannot alias reference to a lifetime identifier\")\n                        }\n                        IdentifierKind::Underscore => bail!(\"Cannot alias reference to `_`\"),\n                        IdentifierKind::LowercaseSelf => {\n                            bail!(\"Cannot rename alias reference to `self`\")\n                        }\n                    };\n                    let mut usages = def.usages(&sema).all();\n\n                    // FIXME: hack - removes the usage that triggered this rename operation.\n                    match usages.references.get_mut(&file_id).and_then(|refs| {\n                        refs.iter()\n                            .position(|ref_| ref_.range.contains_inclusive(position.offset))\n                            .map(|idx| refs.remove(idx))\n                    }) {\n                        Some(_) => (),\n                        None => never!(),\n                    };\n\n                    let mut source_change = SourceChange::default();\n                    source_change.extend(usages.references.get_mut(&file_id).iter().map(|refs| {","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide/src/rename.rs#L155-L191","documentation":"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.","triggerScenarios":"Calling rename with position on a `use` tree path segment and new_name == \"_\" (IdentifierKind::Underscore classification).","commonSituations":"Tools or users attempting to 'anonymize' an import via rename; scripted refactors that pass underscore as a placeholder new name.","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."],"exampleFix":"// before\nrename(use_tree_pos, \"_\")?;\n// after\nrename(use_tree_pos, \"_unused_bar\")?; // use foo::bar as _unused_bar;\n","handlingStrategy":"validation","validationCode":"// Reject bare underscore as a new alias name\nif new_name == \"_\" {\n    return Err(\"alias cannot be `_`\");\n}","typeGuard":"fn is_valid_alias(name: &str) -> bool {\n    name != \"_\" && !name.starts_with('\\'')\n        && name.chars().all(|c| c.is_alphanumeric() || c == '_')\n}","tryCatchPattern":"match rename(db, pos, new_name, &config) {\n    Ok(change) => apply(change),\n    Err(e) if e.to_string().contains(\"Cannot alias reference to `_`\") => {\n        prompt_user(\"Choose a non-underscore alias name\");\n    }\n    Err(e) => report(e),\n}","preventionTips":["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"],"tags":["rust-analyzer","rename","alias","underscore"],"backgroundTag":"invalid-rename-identifier","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"}