Skip to content

Commit 9374d52

Browse files
committed
fix debug tuple structs
1 parent b67378f commit 9374d52

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ enum Foo {
327327
impl core::fmt::Debug for Foo {
328328
$0fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
329329
match self {
330-
Self::Bar(arg1, arg2) => f.debug_tuple("Bar").field(arg1).field(arg2).finish(),
330+
Self::Bar(arg0, arg1) => f.debug_tuple("Bar").field(arg0).field(arg1).finish(),
331331
Self::Baz => write!(f, "Baz"),
332332
}
333333
}

crates/ide_assists/src/utils/gen_trait_fn_body.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
166166
for field in list.fields() {
167167
let name = field.name()?;
168168

169-
// => MyStruct { field_name }
169+
// create a field pattern for use in `MyStruct { fields.. }`
170170
let field_name = field.name()?;
171171
let pat = make::ident_pat(false, false, field_name.clone());
172172
pats.push(pat.into());
@@ -184,11 +184,44 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
184184
let method = make::name_ref("finish");
185185
let expr = make::expr_method_call(expr, method, make::arg_list(None));
186186

187-
// => MyStruct { fields.. } => f.debug_struct()...finish(),
187+
// => MyStruct { fields.. } => f.debug_struct("MyStruct")...finish(),
188188
let pat = make::record_pat(variant_name.clone(), pats.into_iter());
189189
arms.push(make::match_arm(Some(pat.into()), None, expr));
190190
}
191-
Some(ast::FieldList::TupleFieldList(_list)) => todo!(),
191+
Some(ast::FieldList::TupleFieldList(list)) => {
192+
let mut pats = vec![];
193+
194+
// => f.debug_tuple(name)
195+
let target = make::expr_path(make::ext::ident_path("f"));
196+
let method = make::name_ref("debug_tuple");
197+
let struct_name = format!("\"{}\"", name);
198+
let args = make::arg_list(Some(make::expr_literal(&struct_name).into()));
199+
let mut expr = make::expr_method_call(target, method, args);
200+
201+
for (i, _) in list.fields().enumerate() {
202+
let name = format!("arg{}", i);
203+
204+
// create a field pattern for use in `MyStruct(fields..)`
205+
let field_name = make::name(&name);
206+
let pat = make::ident_pat(false, false, field_name.clone());
207+
pats.push(pat.into());
208+
209+
// => <expr>.field(field)
210+
let method_name = make::name_ref("field");
211+
let field_path = &format!("{}", name);
212+
let field_path = make::expr_path(make::ext::ident_path(field_path));
213+
let args = make::arg_list(vec![field_path]);
214+
expr = make::expr_method_call(expr, method_name, args);
215+
}
216+
217+
// => <expr>.finish()
218+
let method = make::name_ref("finish");
219+
let expr = make::expr_method_call(expr, method, make::arg_list(None));
220+
221+
// => MyStruct (fields..) => f.debug_tuple("MyStruct")...finish(),
222+
let pat = make::tuple_struct_pat(variant_name.clone(), pats.into_iter());
223+
arms.push(make::match_arm(Some(pat.into()), None, expr));
224+
}
192225
None => {
193226
let fmt_string = make::expr_literal(&(format!("\"{}\"", name))).into();
194227
let args = make::arg_list(vec![target, fmt_string]);

0 commit comments

Comments
 (0)