Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -145,6 +145,10 @@ Contexts[Contexts.size() - 2].IsExpression && !Line.startsWith(tok::kw_template)) return false; + // If we see a ; then likely this is a for loop and not the template + if (CurrentToken->is(tok::semi)) + return false; + updateParameterCount(Left, CurrentToken); if (Style.Language == FormatStyle::LK_Proto) { if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) { Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -7065,6 +7065,8 @@ verifyFormat("static_assert(is_convertible::value, \"AAA\");"); verifyFormat("Constructor(A... a) : a_(X{std::forward(a)}...) {}"); verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); + + verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); } TEST_F(FormatTest, BitshiftOperatorWidth) {