Skip to content

Commit 3d6d4e9

Browse files
committed
Don't mutate the tree while traversing in reorder_impl
1 parent 544a93e commit 3d6d4e9

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

crates/ide_assists/src/handlers/reorder_impl.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ pub(crate) fn reorder_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
7979
"Sort methods",
8080
target,
8181
|builder| {
82-
methods.into_iter().zip(sorted).for_each(|(old, new)| {
83-
ted::replace(builder.make_ast_mut(old).syntax(), new.clone_for_update().syntax())
84-
});
82+
let methods =
83+
methods.into_iter().map(|fn_| builder.make_ast_mut(fn_)).collect::<Vec<_>>();
84+
methods
85+
.into_iter()
86+
.zip(sorted)
87+
.for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax()));
8588
},
8689
)
8790
}
@@ -160,7 +163,7 @@ $0impl Bar for Foo {}
160163
}
161164

162165
#[test]
163-
fn reorder_impl_trait_methods() {
166+
fn reorder_impl_trait_functions() {
164167
check_assist(
165168
reorder_impl,
166169
r#"
@@ -197,4 +200,33 @@ impl Bar for Foo {
197200
"#,
198201
)
199202
}
203+
204+
#[test]
205+
fn reorder_impl_trait_methods_uneven_ident_lengths() {
206+
check_assist(
207+
reorder_impl,
208+
r#"
209+
trait Bar {
210+
fn foo(&mut self) {}
211+
fn fooo(&mut self) {}
212+
}
213+
214+
struct Foo;
215+
impl Bar for Foo {
216+
fn fooo(&mut self) {}
217+
fn foo(&mut self) {$0}
218+
}"#,
219+
r#"
220+
trait Bar {
221+
fn foo(&mut self) {}
222+
fn fooo(&mut self) {}
223+
}
224+
225+
struct Foo;
226+
impl Bar for Foo {
227+
fn foo(&mut self) {}
228+
fn fooo(&mut self) {}
229+
}"#,
230+
)
231+
}
200232
}

0 commit comments

Comments
 (0)