From 13749e782e307fbd52c1f85112982c93bfd0f2e7 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 9 Aug 2021 18:27:01 +0200 Subject: [PATCH] move code around --- .../replace_derive_with_manual_impl.rs | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index 1bd7040b5f5..bcabca64b32 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs @@ -189,7 +189,7 @@ fn gen_trait_body_impl( match trait_path.segment()?.name_ref()?.text().as_str() { "Debug" => gen_debug_impl(adt, func, annotated_name), "Default" => gen_default_impl(adt, func), - _ => Some(()), + _ => None, } } @@ -197,7 +197,7 @@ fn gen_trait_body_impl( fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn, annotated_name: &ast::Name) -> Option<()> { match adt { // `Debug` cannot be derived for unions, so no default impl can be provided. - ast::Adt::Union(_) => Some(()), + ast::Adt::Union(_) => None, // => match self { Self::Variant => write!(f, "Variant") } ast::Adt::Enum(enum_) => { @@ -279,11 +279,17 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn, annotated_name: &ast::Name) -> /// Generate a `Debug` impl based on the fields and members of the target type. fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { - return match adt { + fn gen_default_call() -> ast::Expr { + let trait_name = make::ext::ident_path("Default"); + let method_name = make::ext::ident_path("default"); + let fn_name = make::expr_path(make::path_concat(trait_name, method_name)); + make::expr_call(fn_name, make::arg_list(None)) + } + match adt { // `Debug` cannot be derived for unions, so no default impl can be provided. - ast::Adt::Union(_) => Some(()), + ast::Adt::Union(_) => None, // Deriving `Debug` for enums is not stable yet. - ast::Adt::Enum(_) => Some(()), + ast::Adt::Enum(_) => None, ast::Adt::Struct(strukt) => { let expr = match strukt.field_list() { Some(ast::FieldList::RecordFieldList(field_list)) => { @@ -311,15 +317,8 @@ fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { }; let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1)); ted::replace(func.body()?.syntax(), body.clone_for_update().syntax()); - return Some(()); + Some(()) } - }; - - fn gen_default_call() -> ast::Expr { - let trait_name = make::ext::ident_path("Default"); - let method_name = make::ext::ident_path("default"); - let fn_name = make::expr_path(make::path_concat(trait_name, method_name)); - make::expr_call(fn_name, make::arg_list(None)) } } fn update_attribute( -- 2.44.0