{"record":{"id":"6b96a9d28a0e96b6","repo":"rust-lang/rust-analyzer","slug":"invalid-name-not-a-lifetime-identifier","errorCode":null,"errorMessage":"Invalid name `{}`: not a lifetime identifier","messagePattern":"Invalid name `(.+?)`: not a lifetime identifier","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide-db/src/rename.rs","lineNumber":364,"sourceCode":"}\n\nfn rename_reference<'db>(\n    sema: &Semantics<'db, RootDatabase>,\n    def: Definition<'db>,\n    new_name: &str,\n    rename_definition: RenameDefinition,\n    edition: Edition,\n    config: &RenameConfig,\n) -> Result<SourceChange> {\n    let (mut new_name, ident_kind) = IdentifierKind::classify(edition, new_name)?;\n\n    if matches!(\n        def,\n        Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_)\n    ) {\n        match ident_kind {\n            IdentifierKind::Underscore => {\n                bail!(\n                    \"Invalid name `{}`: not a lifetime identifier\",\n                    new_name.display(sema.db, edition)\n                );\n            }\n            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));","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide-db/src/rename.rs#L346-L382","documentation":"In `rename_reference`, when the definition being renamed is a lifetime parameter or label, the new name must classify as `Lifetime` or `Ident` (an apostrophe is auto-added). Passing `_` classifies as `Underscore`, which is not a valid lifetime identifier, so the rename bails with \"Invalid name `{}`: not a lifetime identifier\".","triggerScenarios":"Renaming a `Definition::GenericParam(LifetimeParam)` or `Definition::Label` while passing the new name `\"_\"` (classified as `IdentifierKind::Underscore`) at crates/ide-db/src/rename.rs:364.","commonSituations":"User attempts to rename `'a` to `_` in `fn f<'a>(x: &'a str)` or to rename a loop label `'lbl` to `_`; the IDE rejects the operation.","solutions":["Supply a lifetime-shaped name such as `'b` (or plain `b`, which gets an apostrophe added automatically).","Avoid `_` as a rename target for lifetimes and labels.","In the client, validate that rename input for lifetimes matches `'?[a-z_][A-Za-z0-9_]*` before calling."],"exampleFix":"// before\nrename(lifetime_def, \"_\")?; // Err: not a lifetime identifier\n// after\nrename(lifetime_def, \"'b\")?; // ok","handlingStrategy":"validation","validationCode":"fn is_valid_lifetime_input(name: &str) -> bool {\n    let s = name.strip_prefix('\\'').unwrap_or(name);\n    !s.is_empty() && s != \"_\" && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')\n}","typeGuard":"fn is_lifetime_target(def: &Definition) -> bool {\n    matches!(def, Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_))\n}","tryCatchPattern":"match def.rename(sema, new_name, ...) {\n    Err(e) if e.to_string().contains(\"not a lifetime identifier\") => eprintln!(\"use a lifetime name like 'b\"),\n    other => other?,\n}","preventionTips":["For lifetime/label targets, restrict input to `'ident` or plain ident (apostrophe added automatically).","Never offer `_` as a rename target for lifetimes or labels.","Validate the classification (IdentifierKind) client-side before calling rename."],"tags":["rust","ide","rename","lifetimes"],"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"}