@@ -1103,7 +1103,10 @@ fn clean_function<'tcx>(
11031103 let generics = clean_generics ( generics, cx) ;
11041104 let params = match params {
11051105 ParamsSrc :: Body ( body_id) => clean_params_via_body ( cx, sig. decl . inputs , body_id) ,
1106- ParamsSrc :: Idents ( idents) => clean_params ( cx, sig. decl . inputs , idents) ,
1106+ // Let's not perpetuate anon params from Rust 2015; use `_` for them.
1107+ ParamsSrc :: Idents ( idents) => clean_params ( cx, sig. decl . inputs , idents, |ident| {
1108+ Some ( ident. map_or ( kw:: Underscore , |ident| ident. name ) )
1109+ } ) ,
11071110 } ;
11081111 let decl = clean_fn_decl_with_params ( cx, sig. decl , Some ( & sig. header ) , params) ;
11091112 ( generics, decl)
@@ -1115,30 +1118,13 @@ fn clean_params<'tcx>(
11151118 cx : & mut DocContext < ' tcx > ,
11161119 types : & [ hir:: Ty < ' tcx > ] ,
11171120 idents : & [ Option < Ident > ] ,
1121+ postprocess : impl Fn ( Option < Ident > ) -> Option < Symbol > ,
11181122) -> Vec < Parameter > {
1119- fn nonempty_name ( ident : & Option < Ident > ) -> Option < Symbol > {
1120- if let Some ( ident) = ident
1121- && ident. name != kw:: Underscore
1122- {
1123- Some ( ident. name )
1124- } else {
1125- None
1126- }
1127- }
1128-
1129- // If at least one argument has a name, use `_` as the name of unnamed
1130- // arguments. Otherwise omit argument names.
1131- let default_name = if idents. iter ( ) . any ( |ident| nonempty_name ( ident) . is_some ( ) ) {
1132- Some ( kw:: Underscore )
1133- } else {
1134- None
1135- } ;
1136-
11371123 types
11381124 . iter ( )
11391125 . enumerate ( )
11401126 . map ( |( i, ty) | Parameter {
1141- name : idents . get ( i ) . and_then ( nonempty_name ) . or ( default_name ) ,
1127+ name : postprocess ( idents [ i ] ) ,
11421128 type_ : clean_ty ( ty, cx) ,
11431129 is_const : false ,
11441130 } )
@@ -1184,8 +1170,6 @@ fn clean_poly_fn_sig<'tcx>(
11841170 did : Option < DefId > ,
11851171 sig : ty:: PolyFnSig < ' tcx > ,
11861172) -> FnDecl {
1187- let mut names = did. map_or ( & [ ] as & [ _ ] , |did| cx. tcx . fn_arg_idents ( did) ) . iter ( ) ;
1188-
11891173 let mut output = clean_middle_ty ( sig. output ( ) , cx, None , None ) ;
11901174
11911175 // If the return type isn't an `impl Trait`, we can safely assume that this
@@ -1198,16 +1182,20 @@ fn clean_poly_fn_sig<'tcx>(
11981182 output = output. sugared_async_return_type ( ) ;
11991183 }
12001184
1185+ let mut idents = did. map ( |did| cx. tcx . fn_arg_idents ( did) ) . unwrap_or_default ( ) . iter ( ) . copied ( ) ;
1186+
1187+ // If this comes from a fn item, let's not perpetuate anon params from Rust 2015; use `_` for them.
1188+ // If this comes from a fn ptr ty, we just keep params unnamed since it's more conventional stylistically.
1189+ // Since the param name is not part of the semantic type, these params never bear a name unlike
1190+ // in the HIR case, thus we can't peform any fancy fallback logic unlike `clean_bare_fn_ty`.
1191+ let fallback = did. map ( |_| kw:: Underscore ) ;
1192+
12011193 let params = sig
12021194 . inputs ( )
12031195 . iter ( )
1204- . map ( |t| Parameter {
1205- type_ : clean_middle_ty ( t. map_bound ( |t| * t) , cx, None , None ) ,
1206- name : Some ( if let Some ( Some ( ident) ) = names. next ( ) {
1207- ident. name
1208- } else {
1209- kw:: Underscore
1210- } ) ,
1196+ . map ( |ty| Parameter {
1197+ name : idents. next ( ) . flatten ( ) . map ( |ident| ident. name ) . or ( fallback) ,
1198+ type_ : clean_middle_ty ( ty. map_bound ( |ty| * ty) , cx, None , None ) ,
12111199 is_const : false ,
12121200 } )
12131201 . collect ( ) ;
@@ -2597,7 +2585,17 @@ fn clean_bare_fn_ty<'tcx>(
25972585 . filter ( |p| !is_elided_lifetime ( p) )
25982586 . map ( |x| clean_generic_param ( cx, None , x) )
25992587 . collect ( ) ;
2600- let params = clean_params ( cx, bare_fn. decl . inputs , bare_fn. param_idents ) ;
2588+ // Since it's more conventional stylistically, elide the name of all params called `_`
2589+ // unless there's at least one interestingly named param in which case don't elide any
2590+ // name since mixing named and unnamed params is less legible.
2591+ let filter = |ident : Option < Ident > | {
2592+ ident. map ( |ident| ident. name ) . filter ( |& ident| ident != kw:: Underscore )
2593+ } ;
2594+ let fallback =
2595+ bare_fn. param_idents . iter ( ) . copied ( ) . find_map ( filter) . map ( |_| kw:: Underscore ) ;
2596+ let params = clean_params ( cx, bare_fn. decl . inputs , bare_fn. param_idents , |ident| {
2597+ filter ( ident) . or ( fallback)
2598+ } ) ;
26012599 let decl = clean_fn_decl_with_params ( cx, bare_fn. decl , None , params) ;
26022600 ( generic_params, decl)
26032601 } ) ;
0 commit comments