{"record":{"id":"5e95f9d13f01e5c3","repo":"rust-lang/rust-analyzer","slug":"cannot-rename-local-to-self-outside-of-function","errorCode":null,"errorMessage":"Cannot rename local to self outside of function","messagePattern":"Cannot rename local to self outside of function","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide/src/rename.rs","lineNumber":506,"sourceCode":"            source_change.insert_source_edit(\n                replace_range.file_id.file_id(sema.db),\n                TextEdit::replace(replace_range.range, replacement),\n            );\n        }\n    }\n}\n\nfn rename_to_self<'db>(\n    sema: &Semantics<'db, RootDatabase>,\n    local: hir::Local<'db>,\n) -> RenameResult<SourceChange> {\n    if never!(local.is_self(sema.db)) {\n        bail!(\"rename_to_self invoked on self\");\n    }\n\n    let fn_def = match local.parent(sema.db) {\n        hir::ExpressionStoreOwner::Body(hir::DefWithBody::Function(func)) => func,\n        _ => bail!(\"Cannot rename local to self outside of function\"),\n    };\n\n    if fn_def.self_param(sema.db).is_some() {\n        bail!(\"Method already has a self parameter\");\n    }\n\n    let params = fn_def.assoc_fn_params(sema.db);\n    let first_param = params\n        .first()\n        .ok_or_else(|| format_err!(\"Cannot rename local to self unless it is a parameter\"))?;\n    match first_param.as_local(sema.db) {\n        Some(plocal) => {\n            if plocal != local {\n                bail!(\"Only the first parameter may be renamed to self\");\n            }\n        }\n        None => bail!(\"rename_to_self invoked on destructuring parameter\"),\n    }","sourceCodeStart":488,"sourceCodeEnd":524,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide/src/rename.rs#L488-L524","documentation":"rename_to_self converts a function parameter local into a `self` parameter, which is only possible when the local belongs to a function body (a DefWithBody::Function). If the local's parent is any other owner (const, static, closure-only contexts, etc.), the function bails with this error because there is no enclosing fn to attach a self parameter to.","triggerScenarios":"Calling rename with new_name == \"self\" on a local variable whose parent is not a function body (e.g. a let binding inside a const/static, or a local in a non-fn body owner).","commonSituations":"Users pressing F2 on a variable inside a const or static item and typing `self`; tools attempting self-conversion refactors outside impl fns.","solutions":["Only attempt self-rename on parameters of functions defined inside impl blocks.","Rename the local to a normal identifier instead of `self`.","If a self parameter is desired, restructure the code manually: move the logic into an impl fn and add `self`."],"exampleFix":"// before\nconst X: i32 = { let v = 1; v }; // rename v -> self: error\n// after\nimpl Foo { fn method(&self) { let v = 1; } }\n","handlingStrategy":"validation","validationCode":"// Only attempt rename-to-self on locals inside fn bodies\n// Cursor local must be inside a function (not const/static) and not itself `self`\nfn can_rename_to_self(local_parent_kind: &str) -> bool {\n    local_parent_kind == \"function\"\n}","typeGuard":null,"tryCatchPattern":"match rename(db, pos, \"self\", &config) {\n    Ok(change) => apply(change),\n    Err(e) if e.to_string().contains(\"Cannot rename local to self outside of function\") => {\n        prompt_user(\"self rename is only valid inside functions\");\n    }\n    Err(e) => report(e),\n}","preventionTips":["Only expose rename-to-self inside function bodies","Never target `self` itself for rename (that takes the self_param path)","Restrict the rename-to-self refactor to parameters of impl functions"],"tags":["rust-analyzer","rename","self-parameter","validation"],"backgroundTag":"invalid-rename-to-self","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"}