@@ -4823,13 +4823,27 @@ bool OutOfOrderArgumentFailure::diagnoseAsError() {
4823
4823
auto text = SM.extractText (
4824
4824
Lexer::getCharSourceRangeFromSourceRange (SM, firstRange));
4825
4825
4826
- auto removalRange =
4827
- SourceRange (Lexer::getLocForEndOfToken (
4828
- SM, tuple->getElement (ArgIdx - 1 )->getEndLoc ()),
4829
- firstRange.End );
4826
+ SourceLoc removalStartLoc;
4827
+ // For the first argument, start is always next token after `(`.
4828
+ if (ArgIdx == 0 ) {
4829
+ removalStartLoc = tuple->getLParenLoc ();
4830
+ } else {
4831
+ // For all other arguments, start is the next token past
4832
+ // the previous argument.
4833
+ removalStartLoc = tuple->getElement (ArgIdx - 1 )->getEndLoc ();
4834
+ }
4835
+
4836
+ SourceRange removalRange{Lexer::getLocForEndOfToken (SM, removalStartLoc),
4837
+ firstRange.End };
4838
+
4839
+ // Move requires postfix comma only if argument is moved in-between
4840
+ // other arguments.
4841
+ bool requiresComma = !isExpr<BinaryExpr>(anchor) &&
4842
+ PrevArgIdx != tuple->getNumElements () - 1 ;
4843
+
4830
4844
diag.fixItRemove (removalRange);
4831
4845
diag.fixItInsert (secondRange.Start ,
4832
- text.str () + (isExpr<BinaryExpr>(anchor) ? " " : " , " ));
4846
+ text.str () + (requiresComma ? " , " : " " ));
4833
4847
};
4834
4848
4835
4849
// There are 4 diagnostic messages variations depending on
0 commit comments