11//! This module contains functions to generate default trait impl function bodies where possible.
22
33use hir:: TraitRef ;
4- use syntax:: ast:: { self , AstNode , BinaryOp , CmpOp , HasName , LogicOp , edit:: AstNodeEdit , syntax_factory:: SyntaxFactory } ;
4+ use syntax:: ast:: {
5+ self , AstNode , BinaryOp , CmpOp , HasName , LogicOp , edit:: AstNodeEdit ,
6+ syntax_factory:: SyntaxFactory ,
7+ } ;
58
69/// Generate custom trait bodies without default implementation where possible.
710///
@@ -94,8 +97,7 @@ fn gen_clone_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr
9497 }
9598 let pat = make. tuple_struct_pat ( variant_name. clone ( ) , pats. into_iter ( ) ) ;
9699 let struct_name = make. expr_path ( variant_name) ;
97- let tuple_expr =
98- make. expr_call ( struct_name, make. arg_list ( fields) ) . into ( ) ;
100+ let tuple_expr = make. expr_call ( struct_name, make. arg_list ( fields) ) . into ( ) ;
99101 arms. push ( make. match_arm ( pat. into ( ) , None , tuple_expr) ) ;
100102 }
101103
@@ -197,8 +199,7 @@ fn gen_debug_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr
197199
198200 // => <expr>.finish()
199201 let method = make. name_ref ( "finish" ) ;
200- let expr =
201- make. expr_method_call ( expr, method, make. arg_list ( [ ] ) ) . into ( ) ;
202+ let expr = make. expr_method_call ( expr, method, make. arg_list ( [ ] ) ) . into ( ) ;
202203
203204 // => MyStruct { fields.. } => f.debug_struct("MyStruct")...finish(),
204205 let pat_field_list = make. record_pat_field_list ( pats, None ) ;
@@ -232,19 +233,16 @@ fn gen_debug_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr
232233
233234 // => <expr>.finish()
234235 let method = make. name_ref ( "finish" ) ;
235- let expr=
236- make. expr_method_call ( expr, method, make. arg_list ( [ ] ) ) . into ( ) ;
236+ let expr = make. expr_method_call ( expr, method, make. arg_list ( [ ] ) ) . into ( ) ;
237237
238238 // => MyStruct (fields..) => f.debug_tuple("MyStruct")...finish(),
239239 let pat = make. tuple_struct_pat ( variant_name. clone ( ) , pats. into_iter ( ) ) ;
240240 arms. push ( make. match_arm ( pat. into ( ) , None , expr) ) ;
241241 }
242242 None => {
243- let fmt_string =
244- make. expr_literal ( & ( format ! ( "\" {name}\" " ) ) ) . into ( ) ;
245- let args = make. token_tree_from_node (
246- make. arg_list ( [ target, fmt_string] ) . syntax ( ) ,
247- ) ;
243+ let fmt_string = make. expr_literal ( & ( format ! ( "\" {name}\" " ) ) ) . into ( ) ;
244+ let args =
245+ make. token_tree_from_node ( make. arg_list ( [ target, fmt_string] ) . syntax ( ) ) ;
248246 let macro_name = make. ident_path ( "write" ) ;
249247 let macro_call = make. expr_macro ( macro_name, args) ;
250248
@@ -278,8 +276,7 @@ fn gen_debug_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr
278276 let mut expr = make. expr_method_call ( target, method, args) . into ( ) ;
279277 for field in field_list. fields ( ) {
280278 let name = field. name ( ) ?;
281- let f_name =
282- make. expr_literal ( & ( format ! ( "\" {name}\" " ) ) ) . into ( ) ;
279+ let f_name = make. expr_literal ( & ( format ! ( "\" {name}\" " ) ) ) . into ( ) ;
283280 let f_path = make. expr_path ( make. ident_path ( "self" ) ) ;
284281 let f_path = make. expr_field ( f_path, & format ! ( "{name}" ) ) . into ( ) ;
285282 let f_path = make. expr_ref ( f_path, false ) ;
@@ -298,9 +295,7 @@ fn gen_debug_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr
298295 let f_path = make. expr_field ( f_path, & format ! ( "{i}" ) ) . into ( ) ;
299296 let f_path = make. expr_ref ( f_path, false ) ;
300297 let method = make. name_ref ( "field" ) ;
301- expr = make
302- . expr_method_call ( expr, method, make. arg_list ( [ f_path] ) )
303- . into ( ) ;
298+ expr = make. expr_method_call ( expr, method, make. arg_list ( [ f_path] ) ) . into ( ) ;
304299 }
305300 expr
306301 }
@@ -354,9 +349,8 @@ fn gen_default_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockEx
354349 make. record_expr ( struct_name, fields) . into ( )
355350 }
356351 } ;
357- let body = make
358- . block_expr ( None :: < ast:: Stmt > , Some ( expr) )
359- . indent ( ast:: edit:: IndentLevel ( 1 ) ) ;
352+ let body =
353+ make. block_expr ( None :: < ast:: Stmt > , Some ( expr) ) . indent ( ast:: edit:: IndentLevel ( 1 ) ) ;
360354 Some ( body)
361355 }
362356 }
@@ -391,8 +385,7 @@ fn gen_hash_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr>
391385 let mut stmts = vec ! [ ] ;
392386 for field in field_list. fields ( ) {
393387 let base = make. expr_path ( make. ident_path ( "self" ) ) ;
394- let target =
395- make. expr_field ( base, & field. name ( ) ?. to_string ( ) ) . into ( ) ;
388+ let target = make. expr_field ( base, & field. name ( ) ?. to_string ( ) ) . into ( ) ;
396389 stmts. push ( gen_hash_call ( target) ) ;
397390 }
398391 make. block_expr ( stmts, None ) . indent ( ast:: edit:: IndentLevel ( 1 ) )
0 commit comments