@@ -1013,6 +1013,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1013
1013
let mut only_extras_so_far = errors
1014
1014
. peek ( )
1015
1015
. is_some_and ( |first| matches ! ( first, Error :: Extra ( arg_idx) if arg_idx. index( ) == 0 ) ) ;
1016
+ let mut prev_extra_idx = None ;
1016
1017
let mut suggestions = vec ! [ ] ;
1017
1018
while let Some ( error) = errors. next ( ) {
1018
1019
only_extras_so_far &= matches ! ( error, Error :: Extra ( _) ) ;
@@ -1074,11 +1075,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1074
1075
// fn f() {}
1075
1076
// - f(0, 1,)
1076
1077
// + f()
1077
- if only_extras_so_far
1078
- && !errors
1079
- . peek ( )
1080
- . is_some_and ( |next_error| matches ! ( next_error, Error :: Extra ( _) ) )
1081
- {
1078
+ let trim_next_comma = match errors. peek ( ) {
1079
+ Some ( Error :: Extra ( provided_idx) )
1080
+ if only_extras_so_far
1081
+ && provided_idx. index ( ) > arg_idx. index ( ) + 1 =>
1082
+ // If the next Error::Extra ("next") doesn't next to current ("current"),
1083
+ // fn foo(_: (), _: u32) {}
1084
+ // - foo("current", (), 1u32, "next")
1085
+ // + foo((), 1u32)
1086
+ // If the previous error is not a `Error::Extra`, then do not trim the next comma
1087
+ // - foo((), "current", 42u32, "next")
1088
+ // + foo((), 42u32)
1089
+ {
1090
+ prev_extra_idx. map_or ( true , |prev_extra_idx| {
1091
+ prev_extra_idx + 1 == arg_idx. index ( )
1092
+ } )
1093
+ }
1094
+ // If no error left, we need to delete the next comma
1095
+ None if only_extras_so_far => true ,
1096
+ // Not sure if other error type need to be handled as well
1097
+ _ => false ,
1098
+ } ;
1099
+
1100
+ if trim_next_comma {
1082
1101
let next = provided_arg_tys
1083
1102
. get ( arg_idx + 1 )
1084
1103
. map ( |& ( _, sp) | sp)
@@ -1098,6 +1117,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1098
1117
SuggestionText :: Remove ( _) => SuggestionText :: Remove ( true ) ,
1099
1118
_ => SuggestionText :: DidYouMean ,
1100
1119
} ;
1120
+ prev_extra_idx = Some ( arg_idx. index ( ) )
1101
1121
}
1102
1122
}
1103
1123
Error :: Missing ( expected_idx) => {
0 commit comments