Index: clang/lib/Format/ContinuationIndenter.cpp =================================================================== --- clang/lib/Format/ContinuationIndenter.cpp +++ clang/lib/Format/ContinuationIndenter.cpp @@ -404,6 +404,7 @@ (State.Column + State.Line->Last->TotalLength - Previous.TotalLength > getColumnLimit(State) || CurrentState.BreakBeforeParameter) && + (!Current.isTrailingComment() || Current.NewlinesBefore > 0) && (Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All || Style.BreakConstructorInitializers != FormatStyle::BCIS_BeforeColon || Style.ColumnLimit != 0)) { @@ -793,6 +794,7 @@ (Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) { CurrentState.LastSpace = State.Column; } else if (Previous.is(TT_CtorInitializerColon) && + (!Current.isTrailingComment() || Current.NewlinesBefore > 0) && Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) { CurrentState.Indent = State.Column; Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -4734,7 +4734,7 @@ // the first list element. Otherwise, it should be placed outside of the // list. return Left.is(BK_BracedInit) || - (Left.is(TT_CtorInitializerColon) && + (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 && Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon); } if (Left.is(tok::question) && Right.is(tok::colon)) @@ -4894,8 +4894,10 @@ if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) return true; - if (Left.is(TT_CtorInitializerColon)) - return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon; + if (Left.is(TT_CtorInitializerColon)) { + return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon && + (!Right.isTrailingComment() || Right.NewlinesBefore > 0); + } if (Right.is(TT_CtorInitializerColon)) return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon; if (Left.is(TT_CtorInitializerComma) && Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -7096,7 +7096,7 @@ " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", OnePerLine); - verifyFormat("SomeClass::Constructor() :\n" + verifyFormat("SomeClass::Constructor() : // NOLINT\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", @@ -7133,6 +7133,9 @@ " // Comment forcing unwanted break.\n" " aaaa(aaaa) {}", Style); + verifyFormat("Constructor() : // NOLINT\n" + " aaaa(aaaa) {}", + Style); Style.ColumnLimit = 0; verifyFormat("SomeClass::Constructor() :\n"