@@ -4812,13 +4812,27 @@ bool OutOfOrderArgumentFailure::diagnoseAsError() {
48124812 auto text = SM.extractText (
48134813 Lexer::getCharSourceRangeFromSourceRange (SM, firstRange));
48144814
4815- auto removalRange =
4816- SourceRange (Lexer::getLocForEndOfToken (
4817- SM, tuple->getElement (ArgIdx - 1 )->getEndLoc ()),
4818- firstRange.End );
4815+ SourceLoc removalStartLoc;
4816+ // For the first argument, start is always next token after `(`.
4817+ if (ArgIdx == 0 ) {
4818+ removalStartLoc = tuple->getLParenLoc ();
4819+ } else {
4820+ // For all other arguments, start is the next token past
4821+ // the previous argument.
4822+ removalStartLoc = tuple->getElement (ArgIdx - 1 )->getEndLoc ();
4823+ }
4824+
4825+ SourceRange removalRange{Lexer::getLocForEndOfToken (SM, removalStartLoc),
4826+ firstRange.End };
4827+
4828+ // Move requires postfix comma only if argument is moved in-between
4829+ // other arguments.
4830+ bool requiresComma = !isExpr<BinaryExpr>(anchor) &&
4831+ PrevArgIdx != tuple->getNumElements () - 1 ;
4832+
48194833 diag.fixItRemove (removalRange);
48204834 diag.fixItInsert (secondRange.Start ,
4821- text.str () + (isExpr<BinaryExpr>(anchor) ? " " : " , " ));
4835+ text.str () + (requiresComma ? " , " : " " ));
48224836 };
48234837
48244838 // There are 4 diagnostic messages variations depending on
0 commit comments