Skip to content

Commit

Permalink
[clang-format] Don't remove parentheses of fold expressions (#91045)
Browse files Browse the repository at this point in the history
Fixes #90966.

(cherry picked from commit db0ed55)
  • Loading branch information
owenca authored and tstellar committed May 9, 2024
1 parent bce9393 commit 0abb89a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2510,6 +2510,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
assert(FormatTok->is(tok::l_paren) && "'(' expected.");
auto *LeftParen = FormatTok;
bool SeenEqual = false;
bool MightBeFoldExpr = false;
const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace);
nextToken();
do {
Expand All @@ -2521,7 +2522,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
parseChildBlock();
break;
case tok::r_paren:
if (!MightBeStmtExpr && !Line->InMacroBody &&
if (!MightBeStmtExpr && !MightBeFoldExpr && !Line->InMacroBody &&
Style.RemoveParentheses > FormatStyle::RPS_Leave) {
const auto *Prev = LeftParen->Previous;
const auto *Next = Tokens->peekNextToken();
Expand Down Expand Up @@ -2564,6 +2565,10 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
parseBracedList();
}
break;
case tok::ellipsis:
MightBeFoldExpr = true;
nextToken();
break;
case tok::equal:
SeenEqual = true;
if (Style.isCSharp() && FormatTok->is(TT_FatArrow))
Expand Down
9 changes: 9 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26894,15 +26894,24 @@ TEST_F(FormatTest, RemoveParentheses) {
"if ((({ a; })))\n"
" b;",
Style);
verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
"static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
Style);
verifyFormat("return (0);", "return (((0)));", Style);
verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
"return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
Style);

Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
verifyFormat("#define Return0 return (0);", Style);
verifyFormat("return 0;", "return (0);", Style);
verifyFormat("co_return 0;", "co_return ((0));", Style);
verifyFormat("return 0;", "return (((0)));", Style);
verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
"return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
Style);
verifyFormat("inline decltype(auto) f() {\n"
" if (a) {\n"
" return (a);\n"
Expand Down

0 comments on commit 0abb89a

Please sign in to comment.