Fix extract function invalid self param - #20864
Conversation
|
I'm not sure adding the method to the trait is usually going to be what the user wants? |
But it may be appropriate to do so instead of creating a generic function with all trait bounds and trait generic parameters |
|
I think in this case, when the selection contains |
6fb908c to
2fa44fd
Compare
Example
---
```rust
trait Foo {
fn f(&self) -> i32;
fn foo(&self) -> i32 {
$0self.f()+self.f()$0
}
}
```
**Before this PR**
```rust
trait Foo {
fn f(&self) -> i32;
fn foo(&self) -> i32 {
fun_name(self)
}
}
fn $0fun_name(&self) -> i32 {
self.f()+self.f()
}
```
**After this PR**
```rust
trait Foo {
fn f(&self) -> i32;
fn foo(&self) -> i32 {
fun_name(self)
}
}
fn $0fun_name(this: &impl Foo) -> i32 {
this.f()+this.f()
}
```
2fa44fd to
1f95d93
Compare
|
Okay, I have implemented the conversion of a simple case from |
| }; | ||
|
|
||
| // FIXME: make trait arguments | ||
| let trait_name = ast::Trait::cast(insert_after.clone()) |
There was a problem hiding this comment.
That seems brittle. There is no inherent correspondence between the insert_after node and the trait. Rather, pass the trait directly (or its name).
| let name = fun.name.clone(); | ||
| let mut call_expr = if fun.self_param.is_some() { | ||
| let args = fun.params.iter().map(|param| param.to_arg(ctx, fun.mods.edition)); | ||
| let mut call_expr = if fun.make_this_param().is_some() { |
There was a problem hiding this comment.
make_this_param() actually creates the param, that's redundant. Instead have has_this_param() or something.
There was a problem hiding this comment.
A single function facilitates the maintenance of unified judgment logic, and I return a closure to solve redundant make
Example
Before this PR
After this PR