Skip to content

Commit 720508a

Browse files
committed
dedup struct debug impl code
1 parent cc3ff1b commit 720508a

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,16 @@ fn impl_def_from_trait(
178178
Some((impl_def, first_assoc_item))
179179
}
180180

181+
/// Generate a `Debug` impl based on the fields and members of the target type.
181182
fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn, annotated_name: &ast::Name) {
182183
match adt {
183184
ast::Adt::Union(_) => {} // `Debug` cannot be derived for unions, so no default impl can be provided.
184185
ast::Adt::Enum(enum_) => {
185186
if let Some(list) = enum_.variant_list() {
186187
let mut arms = vec![];
187188
for variant in list.variants() {
188-
let name = variant.name().unwrap();
189-
190189
// => Self::<Variant>
190+
let name = variant.name().unwrap();
191191
let first = make::ext::ident_path("Self");
192192
let second = make::ext::ident_path(&format!("{}", name));
193193
let pat = make::path_pat(make::path_concat(first, second));
@@ -199,7 +199,6 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn, annotated_name: &ast::Name) {
199199
let target = make::expr_path(make::ext::ident_path("write"));
200200
let expr = make::expr_macro_call(target, args);
201201

202-
// => Self::<Variant> => write!(f, "<Variant>"),
203202
arms.push(make::match_arm(Some(pat.into()), None, expr.into()));
204203
}
205204

@@ -212,52 +211,43 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn, annotated_name: &ast::Name) {
212211
ted::replace(func.body().unwrap().syntax(), body.clone_for_update().syntax());
213212
}
214213
}
215-
ast::Adt::Struct(strukt) => match strukt.field_list() {
216-
Some(ast::FieldList::RecordFieldList(field_list)) => {
217-
let name = format!("\"{}\"", annotated_name);
218-
let args = make::arg_list(Some(make::expr_literal(&name).into()));
219-
let target = make::expr_path(make::ext::ident_path("f"));
220-
let mut expr = make::expr_method_call(target, "debug_struct", args);
221-
for field in field_list.fields() {
222-
if let Some(name) = field.name() {
223-
let f_name = make::expr_literal(&(format!("\"{}\"", name))).into();
214+
ast::Adt::Struct(strukt) => {
215+
let name = format!("\"{}\"", annotated_name);
216+
let args = make::arg_list(Some(make::expr_literal(&name).into()));
217+
let target = make::expr_path(make::ext::ident_path("f"));
218+
219+
let expr = match strukt.field_list() {
220+
None => make::expr_method_call(target, "debug_struct", args),
221+
Some(ast::FieldList::RecordFieldList(field_list)) => {
222+
let mut expr = make::expr_method_call(target, "debug_struct", args);
223+
for field in field_list.fields() {
224+
if let Some(name) = field.name() {
225+
let f_name = make::expr_literal(&(format!("\"{}\"", name))).into();
226+
let f_path = make::expr_path(make::ext::ident_path("self"));
227+
let f_path = make::expr_ref(f_path, false);
228+
let f_path = make::expr_field(f_path, &format!("{}", name)).into();
229+
let args = make::arg_list(vec![f_name, f_path]);
230+
expr = make::expr_method_call(expr, "field", args);
231+
}
232+
}
233+
expr
234+
}
235+
Some(ast::FieldList::TupleFieldList(field_list)) => {
236+
let mut expr = make::expr_method_call(target, "debug_tuple", args);
237+
for (idx, _) in field_list.fields().enumerate() {
224238
let f_path = make::expr_path(make::ext::ident_path("self"));
225239
let f_path = make::expr_ref(f_path, false);
226-
let f_path = make::expr_field(f_path, &format!("{}", name)).into();
227-
let args = make::arg_list(vec![f_name, f_path]);
228-
expr = make::expr_method_call(expr, "field", args);
240+
let f_path = make::expr_field(f_path, &format!("{}", idx)).into();
241+
expr = make::expr_method_call(expr, "field", make::arg_list(Some(f_path)));
229242
}
243+
expr
230244
}
231-
let expr = make::expr_method_call(expr, "finish", make::arg_list(None));
232-
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
233-
ted::replace(func.body().unwrap().syntax(), body.clone_for_update().syntax());
234-
}
235-
Some(ast::FieldList::TupleFieldList(field_list)) => {
236-
let name = format!("\"{}\"", annotated_name);
237-
let args = make::arg_list(Some(make::expr_literal(&name).into()));
238-
let target = make::expr_path(make::ext::ident_path("f"));
239-
let mut expr = make::expr_method_call(target, "debug_tuple", args);
240-
for (idx, _) in field_list.fields().enumerate() {
241-
let f_path = make::expr_path(make::ext::ident_path("self"));
242-
let f_path = make::expr_ref(f_path, false);
243-
let f_path = make::expr_field(f_path, &format!("{}", idx)).into();
244-
let args = make::arg_list(Some(f_path));
245-
expr = make::expr_method_call(expr, "field", args);
246-
}
247-
let expr = make::expr_method_call(expr, "finish", make::arg_list(None));
248-
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
249-
ted::replace(func.body().unwrap().syntax(), body.clone_for_update().syntax());
250-
}
251-
None => {
252-
let name = format!("\"{}\"", annotated_name);
253-
let args = make::arg_list(Some(make::expr_literal(&name).into()));
254-
let target = make::expr_path(make::ext::ident_path("f"));
255-
let expr = make::expr_method_call(target, "debug_struct", args);
256-
let expr = make::expr_method_call(expr, "finish", make::arg_list(None));
257-
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
258-
ted::replace(func.body().unwrap().syntax(), body.clone_for_update().syntax());
259-
}
260-
},
245+
};
246+
247+
let expr = make::expr_method_call(expr, "finish", make::arg_list(None));
248+
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
249+
ted::replace(func.body().unwrap().syntax(), body.clone_for_update().syntax());
250+
}
261251
}
262252
}
263253

0 commit comments

Comments
 (0)