@@ -1097,6 +1097,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10971097 let mut only_extras_so_far = errors
10981098 . peek ( )
10991099 . is_some_and ( |first| matches ! ( first, Error :: Extra ( arg_idx) if arg_idx. index( ) == 0 ) ) ;
1100+ let mut prev_extra_idx = None ;
11001101 let mut suggestions = vec ! [ ] ;
11011102 while let Some ( error) = errors. next ( ) {
11021103 only_extras_so_far &= matches ! ( error, Error :: Extra ( _) ) ;
@@ -1165,11 +1166,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11651166 // fn f() {}
11661167 // - f(0, 1,)
11671168 // + 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 {
11731192 let next = provided_arg_tys
11741193 . get ( arg_idx + 1 )
11751194 . map ( |& ( _, sp) | sp)
@@ -1192,6 +1211,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11921211 SuggestionText :: Remove ( _) => SuggestionText :: Remove ( true ) ,
11931212 _ => SuggestionText :: DidYouMean ,
11941213 } ;
1214+ prev_extra_idx = Some ( arg_idx. index ( ) )
11951215 }
11961216 }
11971217 Error :: Missing ( expected_idx) => {
0 commit comments