Skip to content

Commit db0ed55

Browse files
authored
[clang-format] Don't remove parentheses of fold expressions (#91045)
Fixes #90966.
1 parent c609043 commit db0ed55

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
@@ -2511,6 +2511,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25112511
assert(FormatTok->is(tok::l_paren) && "'(' expected.");
25122512
auto *LeftParen = FormatTok;
25132513
bool SeenEqual = false;
2514+
bool MightBeFoldExpr = false;
25142515
const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace);
25152516
nextToken();
25162517
do {
@@ -2522,7 +2523,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25222523
parseChildBlock();
25232524
break;
25242525
case tok::r_paren:
2525-
if (!MightBeStmtExpr && !Line->InMacroBody &&
2526+
if (!MightBeStmtExpr && !MightBeFoldExpr && !Line->InMacroBody &&
25262527
Style.RemoveParentheses > FormatStyle::RPS_Leave) {
25272528
const auto *Prev = LeftParen->Previous;
25282529
const auto *Next = Tokens->peekNextToken();
@@ -2565,6 +2566,10 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25652566
parseBracedList();
25662567
}
25672568
break;
2569+
case tok::ellipsis:
2570+
MightBeFoldExpr = true;
2571+
nextToken();
2572+
break;
25682573
case tok::equal:
25692574
SeenEqual = true;
25702575
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
@@ -27204,15 +27204,24 @@ TEST_F(FormatTest, RemoveParentheses) {
2720427204
"if ((({ a; })))\n"
2720527205
" b;",
2720627206
Style);
27207+
verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
27208+
"static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
27209+
Style);
2720727210
verifyFormat("return (0);", "return (((0)));", Style);
2720827211
verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
27212+
verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
27213+
"return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27214+
Style);
2720927215

2721027216
Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
2721127217
verifyFormat("#define Return0 return (0);", Style);
2721227218
verifyFormat("return 0;", "return (0);", Style);
2721327219
verifyFormat("co_return 0;", "co_return ((0));", Style);
2721427220
verifyFormat("return 0;", "return (((0)));", Style);
2721527221
verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
27222+
verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
27223+
"return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27224+
Style);
2721627225
verifyFormat("inline decltype(auto) f() {\n"
2721727226
" if (a) {\n"
2721827227
" return (a);\n"

0 commit comments

Comments
 (0)