{"record":{"id":"4931bc74340c85c9","repo":"rust-lang/rust-analyzer","slug":"only-the-first-parameter-may-be-renamed-to-self","errorCode":null,"errorMessage":"Only the first parameter may be renamed to self","messagePattern":"Only the first parameter may be renamed to self","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide/src/rename.rs","lineNumber":520,"sourceCode":"    }\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    }\n\n    let assoc_item = fn_def\n        .as_assoc_item(sema.db)\n        .ok_or_else(|| format_err!(\"Cannot rename parameter to self for free function\"))?;\n    let impl_ = match assoc_item.container(sema.db) {\n        hir::AssocItemContainer::Trait(_) => {\n            bail!(\"Cannot rename parameter to self for trait functions\");\n        }\n        hir::AssocItemContainer::Impl(impl_) => impl_,\n    };\n    let first_param_ty = first_param.ty();\n    let impl_ty = impl_.self_ty(sema.db);\n    let (ty, self_param) = if impl_ty.is_reference() {\n        // if the impl is a ref to the type we can just match the `&T` with self directly","sourceCodeStart":502,"sourceCodeEnd":538,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide/src/rename.rs#L502-L538","documentation":"Renaming to `self` is only allowed for the first parameter of the function. rename_to_self resolves the first parameter's local and, if it differs from the local under the cursor, bails with this error. Self parameters must occupy the first position in Rust's parameter list.","triggerScenarios":"Calling rename with new_name == \"self\" on any local/parameter that is not the function's first parameter (e.g. the second or third fn parameter).","commonSituations":"Users pressing F2 on a later parameter and typing `self`, expecting rust-analyzer to reorder parameters — which it refuses to do.","solutions":["Target the first parameter of the function when renaming to self.","Reorder parameters manually so the desired one is first, then rename to self.","Rename the local to another identifier and refactor to self manually."],"exampleFix":"// before\nfn f(a: i32, b: i32) {} // rename b -> self: error\n// after\nfn f(b: i32, a: i32) {} // rename first param b -> self\n","handlingStrategy":"validation","validationCode":"// Only the first parameter may become self; verify the cursor's local\n// is the first named parameter of the enclosing fn before renaming\nfn is_first_param(cursor_param_index: usize) -> bool {\n    cursor_param_index == 0\n}","typeGuard":null,"tryCatchPattern":"match rename(db, pos, \"self\", &config) {\n    Ok(change) => apply(change),\n    Err(e) if e.to_string().contains(\"Only the first parameter may be renamed to self\") => {\n        prompt_user(\"Only the first parameter can become self\");\n    }\n    Err(e) => report(e),\n}","preventionTips":["Enable rename-to-self only when the cursor is on the first fn parameter","Reorder parameters manually before converting one to self","Offer a parameter-refactor assist instead of a raw rename for later params"],"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"}