File tree Expand file tree Collapse file tree
crates/ide-assists/src/handlers Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -148,7 +148,9 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
148148
149149 let enclosing_impl = ctx. find_node_at_offset :: < ast:: Impl > ( ) ;
150150 let cursor_impl = enclosing_impl. filter ( |impl_| {
151- ctx. sema . to_def ( impl_) . map_or ( false , |def| def. self_ty ( ctx. sema . db ) . as_adt ( ) == Some ( adt) )
151+ ctx. sema . to_def ( impl_) . is_some_and ( |def| {
152+ def. self_ty ( ctx. sema . db ) . as_adt ( ) == Some ( adt) && def. trait_ ( ctx. sema . db ) . is_none ( )
153+ } )
152154 } ) ;
153155
154156 let ( impl_, file) = if let Some ( impl_) = cursor_impl {
@@ -3239,6 +3241,84 @@ impl Foo {
32393241 ${0:todo!()}
32403242 }
32413243}
3244+ " ,
3245+ )
3246+ }
3247+
3248+ #[ test]
3249+ fn generate_method_skips_trait_impl_for_inherent ( ) {
3250+ // regression: rust-lang/rust-analyzer#22123
3251+ check_assist (
3252+ generate_function,
3253+ r"
3254+ struct Bar;
3255+
3256+ impl Bar {
3257+ fn func1() {}
3258+ }
3259+
3260+ trait Foo { fn foo(&self); }
3261+
3262+ impl Foo for Bar {
3263+ fn foo(&self) {
3264+ self.func2$0();
3265+ }
3266+ }
3267+ " ,
3268+ r"
3269+ struct Bar;
3270+
3271+ impl Bar {
3272+ fn func1() {}
3273+
3274+ fn func2(&self) ${0:-> _} {
3275+ todo!()
3276+ }
3277+ }
3278+
3279+ trait Foo { fn foo(&self); }
3280+
3281+ impl Foo for Bar {
3282+ fn foo(&self) {
3283+ self.func2();
3284+ }
3285+ }
3286+ " ,
3287+ )
3288+ }
3289+
3290+ #[ test]
3291+ fn generate_method_from_trait_impl_creates_new_inherent_impl ( ) {
3292+ // #22123: no inherent impl exists, so the assist must synthesize one
3293+ // instead of inserting into the trait impl.
3294+ check_assist (
3295+ generate_function,
3296+ r"
3297+ struct Bar;
3298+
3299+ trait Foo { fn foo(&self); }
3300+
3301+ impl Foo for Bar {
3302+ fn foo(&self) {
3303+ self.func2$0();
3304+ }
3305+ }
3306+ " ,
3307+ r"
3308+ struct Bar;
3309+ impl Bar {
3310+ fn func2(&self) ${0:-> _} {
3311+ todo!()
3312+ }
3313+ }
3314+
3315+ trait Foo { fn foo(&self); }
3316+
3317+ impl Foo for Bar {
3318+ fn foo(&self) {
3319+ self.func2();
3320+ }
3321+ }
32423322" ,
32433323 )
32443324 }
You can’t perform that action at this time.
0 commit comments