@@ -1097,6 +1097,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1097
1097
let mut only_extras_so_far = errors
1098
1098
. peek ( )
1099
1099
. is_some_and ( |first| matches ! ( first, Error :: Extra ( arg_idx) if arg_idx. index( ) == 0 ) ) ;
1100
+ let mut prev_extra_idx = None ;
1100
1101
let mut suggestions = vec ! [ ] ;
1101
1102
while let Some ( error) = errors. next ( ) {
1102
1103
only_extras_so_far &= matches ! ( error, Error :: Extra ( _) ) ;
@@ -1165,11 +1166,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1165
1166
// fn f() {}
1166
1167
// - f(0, 1,)
1167
1168
// + f()
1168
- if only_extras_so_far
1169
- && !errors
1170
- . peek ( )
1171
- . is_some_and ( |next_error| matches ! ( next_error, Error :: Extra ( _) ) )
1172
- {
1169
+ let trim_next_comma = match errors. peek ( ) {
1170
+ Some ( Error :: Extra ( provided_idx) )
1171
+ if only_extras_so_far
1172
+ && provided_idx. index ( ) > arg_idx. index ( ) + 1 =>
1173
+ // If the next Error::Extra ("next") doesn't next to current ("current"),
1174
+ // fn foo(_: (), _: u32) {}
1175
+ // - foo("current", (), 1u32, "next")
1176
+ // + foo((), 1u32)
1177
+ // If the previous error is not a `Error::Extra`, then do not trim the next comma
1178
+ // - foo((), "current", 42u32, "next")
1179
+ // + foo((), 42u32)
1180
+ {
1181
+ prev_extra_idx. map_or ( true , |prev_extra_idx| {
1182
+ prev_extra_idx + 1 == arg_idx. index ( )
1183
+ } )
1184
+ }
1185
+ // If no error left, we need to delete the next comma
1186
+ None if only_extras_so_far => true ,
1187
+ // Not sure if other error type need to be handled as well
1188
+ _ => false ,
1189
+ } ;
1190
+
1191
+ if trim_next_comma {
1173
1192
let next = provided_arg_tys
1174
1193
. get ( arg_idx + 1 )
1175
1194
. map ( |& ( _, sp) | sp)
@@ -1192,6 +1211,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1192
1211
SuggestionText :: Remove ( _) => SuggestionText :: Remove ( true ) ,
1193
1212
_ => SuggestionText :: DidYouMean ,
1194
1213
} ;
1214
+ prev_extra_idx = Some ( arg_idx. index ( ) )
1195
1215
}
1196
1216
}
1197
1217
Error :: Missing ( expected_idx) => {
0 commit comments