{"record":{"id":"da2bbd2c07c880d0","repo":"rust-lang/rust-analyzer","slug":"invalid-name-cannot-rename-to-self","errorCode":null,"errorMessage":"Invalid name `{}`: cannot rename to `self`","messagePattern":"Invalid name `(.+?)`: cannot rename to `self`","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide-db/src/rename.rs","lineNumber":387,"sourceCode":"            IdentifierKind::Ident => {\n                new_name = Name::new_lifetime(&format!(\"'{}\", new_name.as_str()))\n            }\n            IdentifierKind::Lifetime => (),\n            IdentifierKind::LowercaseSelf => bail!(\n                \"Invalid name `{}`: not a lifetime identifier\",\n                new_name.display(sema.db, edition)\n            ),\n        }\n    } else {\n        match ident_kind {\n            IdentifierKind::Lifetime => {\n                cov_mark::hit!(rename_not_an_ident_ref);\n                bail!(\"Invalid name `{}`: not an identifier\", new_name.display(sema.db, edition));\n            }\n            IdentifierKind::Ident => cov_mark::hit!(rename_non_local),\n            IdentifierKind::Underscore => (),\n            IdentifierKind::LowercaseSelf => {\n                bail!(\n                    \"Invalid name `{}`: cannot rename to `self`\",\n                    new_name.display(sema.db, edition)\n                );\n            }\n        }\n    }\n\n    let def = convert_to_def_in_trait(sema.db, def);\n    let usages = def.usages(sema).all();\n\n    if !usages.is_empty() && ident_kind == IdentifierKind::Underscore {\n        cov_mark::hit!(rename_underscore_multiple);\n        bail!(\"Cannot rename reference to `_` as it is being referenced multiple times\");\n    }\n    let mut source_change = SourceChange::default();\n    source_change.extend(usages.iter().map(|(file_id, references)| {\n        let edition = file_id.edition(sema.db);\n        (","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide-db/src/rename.rs#L369-L405","documentation":"When renaming a non-lifetime definition, the name `self` classifies as `IdentifierKind::LowercaseSelf` and is explicitly rejected: `self` is a reserved keyword that cannot be used as an identifier for variables, functions, or other items. The error is raised in the `else` branch of `rename_reference` at crates/ide-db/src/rename.rs:387.","triggerScenarios":"Renaming a local variable, parameter, function, or any non-lifetime definition with the new name `\"self\"`.","commonSituations":"User types `self` into the rename prompt for a variable inside a method, confusing the parameter `self` with a renamable name; the IDE refuses because `self` is contextual keyword syntax, not a plain binding.","solutions":["Pick a non-keyword identifier (e.g. `this`, `slf`, `my_self`).","If the goal is to rename a method's `self`/`&self` receiver, that is not supported — rename parameters or the type instead.","Client-side, block inputs matching Rust reserved keywords before calling rename."],"exampleFix":"// before\nrename(local_def, \"self\")?; // Err: cannot rename to `self`\n// after\nrename(local_def, \"this\")?; // ok","handlingStrategy":"validation","validationCode":"if new_name == \"self\" {\n    return Err(anyhow!(\"cannot rename to the reserved keyword `self`\"));\n}","typeGuard":"fn is_keyword(s: &str) -> bool {\n    [\"self\", \"Self\", \"crate\", \"super\", \"fn\", \"let\", \"mut\", \"_\"].contains(&s)\n}","tryCatchPattern":"match def.rename(sema, new_name, ...) {\n    Err(e) if e.to_string().contains(\"cannot rename to `self`\") => eprintln!(\"choose a non-keyword name\"),\n    other => other?,\n}","preventionTips":["Blocklist all Rust keywords in the rename input field.","Do not offer rename on the implicit `self` receiver parameter.","Suggest `slf`/`this` style alternatives."],"tags":["rust","ide","rename","keywords"],"backgroundTag":"invalid-identifier-name","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"}