Skip to content

Commit ef0a1b2

Browse files
Fix tests
1 parent a1c060c commit ef0a1b2

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

crates/assists/src/handlers/replace_impl_trait_with_generic.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
77
// Replaces `impl Trait` function argument with the named generic.
88
//
99
// ```
10-
// fn foo<G>(bar: <|>impl Bar) {}
10+
// fn foo(bar: <|>impl Bar) {}
1111
// ```
1212
// ->
1313
// ```
14-
// fn foo<B: Bar>(bar: B) {}
14+
// fn foo<B: Bar,>(bar: B) {}
1515
// ```
1616
pub(crate) fn replace_impl_trait_with_generic(
1717
acc: &mut Assists,
@@ -21,21 +21,15 @@ pub(crate) fn replace_impl_trait_with_generic(
2121
let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?;
2222
let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?;
2323

24-
let impl_trait_ty = type_impl_trait
25-
.syntax()
26-
.descendants()
27-
.last()
28-
.and_then(ast::NameRef::cast)?
29-
.text()
30-
.to_string();
24+
let impl_trait_ty = type_impl_trait.type_bound_list()?;
3125

3226
let target = type_fn.syntax().text_range();
3327
acc.add(
3428
AssistId("replace_impl_trait_with_generic", AssistKind::RefactorRewrite),
3529
"Replace impl trait with generic",
3630
target,
3731
|edit| {
38-
let generic_letter = impl_trait_ty.chars().next().unwrap().to_string();
32+
let generic_letter = impl_trait_ty.to_string().chars().next().unwrap().to_string();
3933

4034
let generic_param_list = type_fn
4135
.generic_param_list()
@@ -65,7 +59,7 @@ mod tests {
6559
fn foo<G>(bar: <|>impl Bar) {}
6660
"#,
6761
r#"
68-
fn foo<G, B: Bar>(bar: B) {}
62+
fn foo<G, B: Bar,>(bar: B) {}
6963
"#,
7064
);
7165
}
@@ -78,7 +72,7 @@ mod tests {
7872
fn foo(bar: <|>impl Bar) {}
7973
"#,
8074
r#"
81-
fn foo<B: Bar>(bar: B) {}
75+
fn foo<B: Bar,>(bar: B) {}
8276
"#,
8377
);
8478
}
@@ -91,7 +85,7 @@ mod tests {
9185
fn foo<G>(foo: impl Foo, bar: <|>impl Bar) {}
9286
"#,
9387
r#"
94-
fn foo<G, B: Bar>(foo: impl Foo, bar: B) {}
88+
fn foo<G, B: Bar,>(foo: impl Foo, bar: B) {}
9589
"#,
9690
);
9791
}
@@ -104,7 +98,7 @@ mod tests {
10498
fn foo<>(bar: <|>impl Bar) {}
10599
"#,
106100
r#"
107-
fn foo<B: Bar>(bar: B) {}
101+
fn foo<B: Bar,>(bar: B) {}
108102
"#,
109103
);
110104
}
@@ -133,7 +127,7 @@ mod tests {
133127
fn foo<B>(bar: <|>impl Bar) {}
134128
"#,
135129
r#"
136-
fn foo<B, C: Bar>(bar: C) {}
130+
fn foo<B, C: Bar,>(bar: C) {}
137131
"#,
138132
);
139133
}
@@ -158,4 +152,17 @@ mod tests {
158152
"#,
159153
);
160154
}
155+
156+
#[test]
157+
fn replace_impl_trait_multiple() {
158+
check_assist(
159+
replace_impl_trait_with_generic,
160+
r#"
161+
fn foo(bar: <|>impl Foo + Bar) {}
162+
"#,
163+
r#"
164+
fn foo<F: Foo + Bar,>(bar: F) {}
165+
"#,
166+
);
167+
}
161168
}

crates/assists/src/tests/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,10 @@ fn doctest_replace_impl_trait_with_generic() {
819819
check_doc_test(
820820
"replace_impl_trait_with_generic",
821821
r#####"
822-
fn foo<G>(bar: <|>impl Bar) {}
822+
fn foo(bar: <|>impl Bar) {}
823823
"#####,
824824
r#####"
825-
fn foo<B: Bar>(bar: B) {}
825+
fn foo<B: Bar,>(bar: B) {}
826826
"#####,
827827
)
828828
}

crates/syntax/src/ast/make.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList
294294
ast_from_text(&format!("fn f({}) {{ }}", args))
295295
}
296296

297-
pub fn generic_param(name: String, ty: Option<String>) -> ast::GenericParam {
297+
pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {
298298
let bound = match ty {
299299
Some(it) => format!(": {}", it),
300300
None => String::new(),

0 commit comments

Comments
 (0)