Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -2249,6 +2249,9 @@ return 500; } + if (Left.is(tok::coloncolon) || + (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) + return 500; if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || Right.is(tok::kw_operator)) { if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) @@ -2267,9 +2270,6 @@ return 160; if (Left.is(TT_CastRParen)) return 100; - if (Left.is(tok::coloncolon) || - (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) - return 500; if (Left.isOneOf(tok::kw_class, tok::kw_struct)) return 5000; if (Left.is(tok::comment)) Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -4183,6 +4183,18 @@ Style); } +TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { + // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: + // Prefer keeping `::` followed by `operator` together. + EXPECT_EQ("const aaaa::bbbbbbb &\n" + "ccccccccc::operator++() {\n" + " stuff();\n" + "}", + format("const aaaa::bbbbbbb\n" + "&ccccccccc::operator++() { stuff(); }", + getLLVMStyleWithColumns(40))); +} + TEST_F(FormatTest, TrailingReturnType) { verifyFormat("auto foo() -> int;\n"); verifyFormat("struct S {\n"