about summary refs log tree commit diff
diff options
context:
space:
mode:
authorA4-Tacks <wdsjxhno1001@163.com>2025-09-11 10:23:02 +0800
committerA4-Tacks <wdsjxhno1001@163.com>2025-09-11 10:23:02 +0800
commit2d87bc3e6bfc9e6ccda01a5898980b1e0a706708 (patch)
tree9a01305e292a6d73762c56343fef714cd07be3fd
parent8764ecfe99c328b390278608c791f7b3732f3c23 (diff)
downloadrust-2d87bc3e6bfc9e6ccda01a5898980b1e0a706708.tar.gz
rust-2d87bc3e6bfc9e6ccda01a5898980b1e0a706708.zip
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!()
    }
}
```
-rw-r--r--src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_function.rs31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_function.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_function.rs
index 88ed6f9ce12..a9cf2c1bae1 100644
--- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_function.rs
+++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_function.rs
@@ -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,
@@ -2416,6 +2418,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(
             generate_function,