{"record":{"id":"9c9e93b2b867b7f8","repo":"rust-lang/rust-analyzer","slug":"cannot-rename-alias-reference-to-self","errorCode":null,"errorMessage":"Cannot rename alias reference to `self`","messagePattern":"Cannot rename alias reference to `self`","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide/src/rename.rs","lineNumber":175,"sourceCode":"\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| {\n                        (\n                            position.file_id,","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide/src/rename.rs#L157-L193","documentation":"Renaming a `use`-tree alias to `self` is rejected. `self` as LowercaseSelf identifier would produce `use foo::bar as self`, which is not legal Rust (self-imports have dedicated syntax and semantics the alias fallback does not support).","triggerScenarios":"Calling rename with position on a `use` tree path segment (alias_fallback active) and new_name == \"self\" (IdentifierKind::LowercaseSelf).","commonSituations":"Refactor scripts that rename imports to lowercase names; users pressing F2 on an import and typing `self` expecting a `use foo::self` behavior.","solutions":["Choose any identifier other than `self` for the alias.","For importing a module as `self`, write `use foo::self;` or `use foo;` directly in source; rust-analyzer rename does not generate it."],"exampleFix":"// before\nrename(pos, \"self\")?; // error\n// after\nrename(pos, \"foo_module\")?; // use foo::bar as foo_module;\n","handlingStrategy":"validation","validationCode":"// Reject `self` as a new alias name\nif new_name == \"self\" {\n    return Err(\"alias cannot be `self`\");\n}","typeGuard":"fn is_renameable_name(name: &str) -> bool {\n    !matches!(name, \"self\" | \"Self\" | \"_\" | \"crate\" | \"super\")\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 rename alias reference to `self`\") => {\n        prompt_user(\"`self` is not a valid alias name\");\n    }\n    Err(e) => report(e),\n}","preventionTips":["Keep a keyword blacklist (self, Self, crate, super) in rename UI validation","Generate new names from identifier-sanitizing helpers that strip keywords","Test scripted refactors against keyword names before bulk application"],"tags":["rust-analyzer","rename","alias","self-keyword"],"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"}