Skip to content

Commit

Permalink
[clang-format] Fix operator overload inconsistency in `BreakAfterAttr…
Browse files Browse the repository at this point in the history
…ibutes: Always` (#74943)

Fixes #74901
  • Loading branch information
XDeme1 committed Dec 23, 2023
1 parent f8f8926 commit 8097a5d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
20 changes: 9 additions & 11 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,17 +583,15 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
return true;
}

// If the return type spans multiple lines, wrap before the function name.
if (((Current.is(TT_FunctionDeclarationName) &&
!State.Line->ReturnTypeWrapped &&
// Don't break before a C# function when no break after return type.
(!Style.isCSharp() ||
Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) &&
// Don't always break between a JavaScript `function` and the function
// name.
!Style.isJavaScript()) ||
(Current.is(tok::kw_operator) && Previous.isNot(tok::coloncolon))) &&
Previous.isNot(tok::kw_template) && CurrentState.BreakBeforeParameter) {
if (Current.is(TT_FunctionDeclarationName) &&
!State.Line->ReturnTypeWrapped &&
// Don't break before a C# function when no break after return type.
(!Style.isCSharp() ||
Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) &&
// Don't always break between a JavaScript `function` and the function
// name.
!Style.isJavaScript() && Previous.isNot(tok::kw_template) &&
CurrentState.BreakBeforeParameter) {
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26479,6 +26479,19 @@ TEST_F(FormatTest, BreakAfterAttributes) {
"{\n"
"}",
CtorDtorCode, Style);

verifyFormat("struct Foo {\n"
" [[maybe_unused]]\n"
" void operator+();\n"
"};\n"
"[[nodiscard]]\n"
"Foo &operator-(Foo &);",
Style);

Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left;
verifyFormat("[[nodiscard]]\n"
"Foo& operator-(Foo&);",
Style);
}

TEST_F(FormatTest, InsertNewlineAtEOF) {
Expand Down

0 comments on commit 8097a5d

Please sign in to comment.