Fix empty generic param list for generate_function

Example
---
```rust
struct Foo<S>(S);
impl<S> Foo<S> {
    fn foo(&self) {
        self.bar()$0;
    }
}
```

**Before this PR**:

```rust
struct Foo<S>(S);
impl<S> Foo<S> {
    fn foo(&self) {
        self.bar();
    }

    fn bar<>(&self) ${0:-> _} {
        todo!()
    }
}
```

**After this PR**:

```rust
struct Foo<S>(S);
impl<S> Foo<S> {
    fn foo(&self) {
        self.bar();
    }

    fn bar(&self) ${0:-> _} {
        todo!()
    }
}
```
This commit is contained in:
A4-Tacks 2025-09-11 10:23:02 +08:00
parent 8764ecfe99
commit 2d87bc3e6b
No known key found for this signature in database
GPG key ID: DBD861323040663B

View file

@ -364,11 +364,13 @@ impl FunctionBuilder {
Visibility::Crate => Some(make::visibility_pub_crate()),
Visibility::Pub => Some(make::visibility_pub()),
};
let type_params =
self.generic_param_list.filter(|list| list.generic_params().next().is_some());
let fn_def = make::fn_(
None,
visibility,
self.fn_name,
self.generic_param_list,
type_params,
self.where_clause,
self.params,
self.fn_body,
@ -2415,6 +2417,33 @@ impl Foo {
)
}
#[test]
fn create_method_with_unused_generics() {
check_assist(
generate_function,
r#"
struct Foo<S>(S);
impl<S> Foo<S> {
fn foo(&self) {
self.bar()$0;
}
}
"#,
r#"
struct Foo<S>(S);
impl<S> Foo<S> {
fn foo(&self) {
self.bar();
}
fn bar(&self) ${0:-> _} {
todo!()
}
}
"#,
)
}
#[test]
fn create_function_with_async() {
check_assist(