Skip to content

Commit 0abb89a

Browse files
owencatstellar
authored andcommitted
[clang-format] Don't remove parentheses of fold expressions (#91045)
Fixes #90966. (cherry picked from commit db0ed55)
1 parent bce9393 commit 0abb89a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25102510
assert(FormatTok->is(tok::l_paren) && "'(' expected.");
25112511
auto *LeftParen = FormatTok;
25122512
bool SeenEqual = false;
2513+
bool MightBeFoldExpr = false;
25132514
const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace);
25142515
nextToken();
25152516
do {
@@ -2521,7 +2522,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25212522
parseChildBlock();
25222523
break;
25232524
case tok::r_paren:
2524-
if (!MightBeStmtExpr && !Line->InMacroBody &&
2525+
if (!MightBeStmtExpr && !MightBeFoldExpr && !Line->InMacroBody &&
25252526
Style.RemoveParentheses > FormatStyle::RPS_Leave) {
25262527
const auto *Prev = LeftParen->Previous;
25272528
const auto *Next = Tokens->peekNextToken();
@@ -2564,6 +2565,10 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25642565
parseBracedList();
25652566
}
25662567
break;
2568+
case tok::ellipsis:
2569+
MightBeFoldExpr = true;
2570+
nextToken();
2571+
break;
25672572
case tok::equal:
25682573
SeenEqual = true;
25692574
if (Style.isCSharp() && FormatTok->is(TT_FatArrow))

clang/unittests/Format/FormatTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26894,15 +26894,24 @@ TEST_F(FormatTest, RemoveParentheses) {
2689426894
"if ((({ a; })))\n"
2689526895
" b;",
2689626896
Style);
26897+
verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
26898+
"static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
26899+
Style);
2689726900
verifyFormat("return (0);", "return (((0)));", Style);
2689826901
verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
26902+
verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
26903+
"return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
26904+
Style);
2689926905

2690026906
Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
2690126907
verifyFormat("#define Return0 return (0);", Style);
2690226908
verifyFormat("return 0;", "return (0);", Style);
2690326909
verifyFormat("co_return 0;", "co_return ((0));", Style);
2690426910
verifyFormat("return 0;", "return (((0)));", Style);
2690526911
verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
26912+
verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
26913+
"return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
26914+
Style);
2690626915
verifyFormat("inline decltype(auto) f() {\n"
2690726916
" if (a) {\n"
2690826917
" return (a);\n"

0 commit comments

Comments
 (0)