@@ -1103,7 +1103,10 @@ fn clean_function<'tcx>(
1103
1103
let generics = clean_generics ( generics, cx) ;
1104
1104
let params = match params {
1105
1105
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
+ } ) ,
1107
1110
} ;
1108
1111
let decl = clean_fn_decl_with_params ( cx, sig. decl , Some ( & sig. header ) , params) ;
1109
1112
( generics, decl)
@@ -1115,30 +1118,13 @@ fn clean_params<'tcx>(
1115
1118
cx : & mut DocContext < ' tcx > ,
1116
1119
types : & [ hir:: Ty < ' tcx > ] ,
1117
1120
idents : & [ Option < Ident > ] ,
1121
+ postprocess : impl Fn ( Option < Ident > ) -> Option < Symbol > ,
1118
1122
) -> 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
-
1137
1123
types
1138
1124
. iter ( )
1139
1125
. enumerate ( )
1140
1126
. map ( |( i, ty) | Parameter {
1141
- name : idents . get ( i ) . and_then ( nonempty_name ) . or ( default_name ) ,
1127
+ name : postprocess ( idents [ i ] ) ,
1142
1128
type_ : clean_ty ( ty, cx) ,
1143
1129
is_const : false ,
1144
1130
} )
@@ -1184,8 +1170,6 @@ fn clean_poly_fn_sig<'tcx>(
1184
1170
did : Option < DefId > ,
1185
1171
sig : ty:: PolyFnSig < ' tcx > ,
1186
1172
) -> FnDecl {
1187
- let mut names = did. map_or ( & [ ] as & [ _ ] , |did| cx. tcx . fn_arg_idents ( did) ) . iter ( ) ;
1188
-
1189
1173
let mut output = clean_middle_ty ( sig. output ( ) , cx, None , None ) ;
1190
1174
1191
1175
// 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>(
1198
1182
output = output. sugared_async_return_type ( ) ;
1199
1183
}
1200
1184
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
+
1201
1193
let params = sig
1202
1194
. inputs ( )
1203
1195
. 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 ) ,
1211
1199
is_const : false ,
1212
1200
} )
1213
1201
. collect ( ) ;
@@ -2597,7 +2585,17 @@ fn clean_bare_fn_ty<'tcx>(
2597
2585
. filter ( |p| !is_elided_lifetime ( p) )
2598
2586
. map ( |x| clean_generic_param ( cx, None , x) )
2599
2587
. 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
+ } ) ;
2601
2599
let decl = clean_fn_decl_with_params ( cx, bare_fn. decl , None , params) ;
2602
2600
( generic_params, decl)
2603
2601
} ) ;
0 commit comments