Skip to content

Commit 7f70432

Browse files
committed
[Format] Add format check for throwing negative numbers
Summary: The code `throw -1;` is currently formatted by clang-format as `throw - 1;`. This diff adds a fix for this edge case and a test to check for this in the future. For context, I am looking into a related bug in the clang-formatting of coroutine keywords: `co_yield -1;` is also reformatted in this manner as `co_yield - 1;`. A later diff will add these changes and tests for the `co_yield` and `co_return` keywords. Patch by Jonathan Thomas (jonathoma)! Reviewers: modocache, sammccall, Quuxplusone Reviewed By: sammccall Subscribers: cfe-commits Tags: #clang-format, #clang Differential Revision: https://reviews.llvm.org/D69144 llvm-svn: 375258
1 parent 80fe5cf commit 7f70432

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ class AnnotatingParser {
17571757
// Use heuristics to recognize unary operators.
17581758
if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
17591759
tok::question, tok::colon, tok::kw_return,
1760-
tok::kw_case, tok::at, tok::l_brace))
1760+
tok::kw_case, tok::at, tok::l_brace, tok::kw_throw))
17611761
return TT_UnaryOperator;
17621762

17631763
// There can't be two consecutive binary operators.

clang/unittests/Format/FormatTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6912,6 +6912,7 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) {
69126912
verifyFormat("alignof(char);", getGoogleStyle());
69136913

69146914
verifyFormat("return -1;");
6915+
verifyFormat("throw -1;");
69156916
verifyFormat("switch (a) {\n"
69166917
"case -1:\n"
69176918
" break;\n"

0 commit comments

Comments
 (0)