{"record":{"id":"6aef099348315fac","repo":"rust-lang/rust-analyzer","slug":"cannot-rename-parameter-to-self-for-trait-function","errorCode":null,"errorMessage":"Cannot rename parameter to self for trait functions","messagePattern":"Cannot rename parameter to self for trait functions","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide/src/rename.rs","lineNumber":531,"sourceCode":"    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\n        (first_param_ty.clone(), \"self\")\n    } else {\n        first_param_ty.as_reference_inner().map_or((first_param_ty.clone(), \"self\"), |ty| {\n            (ty, if first_param_ty.is_mutable_reference() { \"&mut self\" } else { \"&self\" })\n        })\n    };\n\n    if ty != impl_ty {\n        bail!(\"Parameter type differs from impl block type\");\n    }\n","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide/src/rename.rs#L513-L549","documentation":"rust-analyzer's rename_to_self refuses to convert the first parameter of a trait function into a `self` parameter. A trait's method signature declares its receiver abstractly (e.g. `fn foo(x: &Self)`) and rewriting it to `self` would change the trait contract for every implementor, not just the local function body, so the IDE bails out instead of producing an unsafe edit.","triggerScenarios":"Calling rename with new name \"self\" on the first parameter of a function defined inside a `trait` block, where `fn_def.as_assoc_item()` resolves and `assoc_item.container()` is `AssocItemContainer::Trait(_)`.","commonSituations":"Selecting a parameter like `x: &Self` in a trait default method and invoking rename-to-self (IDE shortcut / `rust-analyzer.rename` from an editor LSP client).","solutions":["Rename the trait method's parameter to a normal identifier instead of `self`.","If a method receiver is wanted, convert a concrete `impl` method's first parameter to `self` rather than the trait declaration's, or edit the trait signature manually (e.g. `fn foo(self)` / `fn foo(&self)`).","Implement the method in an `impl Trait for Type` block and rename that impl's first parameter to `self` there."],"exampleFix":"// before\ntrait Speaker {\n    fn speak(me: &Self);\n}\n// after\ntrait Speaker {\n    fn speak(&self);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match rust_analyzer.rename(new_name == \"self\") {\n    Ok(edit) => apply(edit),\n    Err(e) if e.contains(\"trait functions\") => {\n        // edit the trait signature manually or target an impl method instead\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Only request rename-to-self on methods inside `impl` blocks.","Edit trait method receivers (`self`, `&self`, `&mut self`) directly in source.","Check the enclosing item kind (trait vs impl) before issuing the rename."],"tags":["rust","rename","trait","ide"],"backgroundTag":"rename-to-self-unsupported","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"}