diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 848f69afca7c32..f1ff1995c806d4 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1023,6 +1023,8 @@ class AnnotatingParser { if (Style.isCpp() && CurrentToken && CurrentToken->is(tok::kw_co_await)) next(); Contexts.back().ColonIsForRangeExpr = true; + if (!CurrentToken || CurrentToken->isNot(tok::l_paren)) + return false; next(); if (!parseParens()) return false; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 459cbf1c7681c9..926bf8905c7a6a 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -12113,6 +12113,7 @@ TEST_F(FormatTest, IncorrectCodeMissingParens) { verifyFormat("if {\n foo;\n foo();\n}"); verifyFormat("switch {\n foo;\n foo();\n}"); verifyIncompleteFormat("for {\n foo;\n foo();\n}"); + verifyIncompleteFormat("ERROR: for target;"); verifyFormat("while {\n foo;\n foo();\n}"); verifyFormat("do {\n foo;\n foo();\n} while;"); }