Skip to content

Commit 5751c43

Browse files
committed
[clang-format] Improve UnwrappedLineParser::mightFitOnOneLine()
Account for an r_brace that precedes an "else if" statement when calculating whether the line might fit on one line if the r_brace is removed. Fixes #59778. Differential Revision: https://reviews.llvm.org/D140835
1 parent 179d24d commit 5751c43

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Diff for: clang/lib/Format/UnwrappedLineParser.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,11 @@ bool UnwrappedLineParser::mightFitOnOneLine(
836836
Length -= OpeningBrace->TokenText.size() + 1;
837837
}
838838

839+
if (const auto *FirstToken = Line.First; FirstToken->is(tok::r_brace)) {
840+
assert(!OpeningBrace || OpeningBrace->is(TT_ControlStatementLBrace));
841+
Length -= FirstToken->TokenText.size() + 1;
842+
}
843+
839844
Index = 0;
840845
for (auto &Token : Tokens) {
841846
const auto &SavedToken = SavedTokens[Index++];

Diff for: clang/unittests/Format/BracesRemoverTest.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,17 @@ TEST_F(BracesRemoverTest, RemoveBraces) {
828828
"}",
829829
Style);
830830

831+
verifyFormat("if (foo)\n"
832+
" f();\n"
833+
"else if (bar || baz)\n"
834+
" g();",
835+
"if (foo) {\n"
836+
" f();\n"
837+
"} else if (bar || baz) {\n"
838+
" g();\n"
839+
"}",
840+
Style);
841+
831842
Style.ColumnLimit = 0;
832843
verifyFormat("if (a)\n"
833844
" b234567890223456789032345678904234567890 = "

0 commit comments

Comments
 (0)