-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang-format] Fix a bug in indenting lambda trailing arrows #94560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clang-format Author: None (c8ef) Changesclose: #94181 Full diff: https://github.com/llvm/llvm-project/pull/94560.diff 2 Files Affected:
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 6b9fbfe0ebf53..630a4ebff9843 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1457,6 +1457,11 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
!Current.isOneOf(tok::colon, tok::comment)) {
return ContinuationIndent;
}
+ if (Style.isCpp() && Current.is(tok::arrow) &&
+ Previous.isOneOf(tok::kw_noexcept, tok::kw_mutable, tok::kw_constexpr,
+ tok::kw_consteval, tok::kw_static)) {
+ return ContinuationIndent;
+ }
if (Current.is(TT_ProtoExtensionLSquare))
return CurrentState.Indent;
if (Current.isBinaryOperator() && CurrentState.UnindentOperator) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4e427268fb82a..d117efc06c2a7 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22858,6 +22858,31 @@ TEST_F(FormatTest, FormatsLambdas) {
" //\n"
" });");
+ verifyFormat("int main() {\n"
+ " very_long_function_name_yes_it_is_really_long(\n"
+ " [](auto n)\n"
+ " -> std::unordered_map<very_long_type_name_A, "
+ "very_long_type_name_B> {\n"
+ " really_do_something();\n"
+ " });\n"
+ "}");
+ verifyFormat("int main() {\n"
+ " very_long_function_name_yes_it_is_really_long(\n"
+ " [](auto n) noexcept\n"
+ " -> std::unordered_map<very_long_type_name_A, "
+ "very_long_type_name_B> {\n"
+ " really_do_something();\n"
+ " });\n"
+ "}");
+ verifyFormat("int main() {\n"
+ " very_long_function_name_yes_it_is_really_long(\n"
+ " [](auto n) constexpr\n"
+ " -> std::unordered_map<very_long_type_name_A, "
+ "very_long_type_name_B> {\n"
+ " really_do_something();\n"
+ " });\n"
+ "}");
+
FormatStyle DoNotMerge = getLLVMStyle();
DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
verifyFormat("auto c = []() {\n"
|
@@ -1457,6 +1457,11 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { | |||
!Current.isOneOf(tok::colon, tok::comment)) { | |||
return ContinuationIndent; | |||
} | |||
if (Style.isCpp() && Current.is(tok::arrow) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (Style.isCpp() && Current.is(tok::arrow) && | |
if (Style.isCpp() && Current.is(TT_TrailingReturnArrow) && |
This is what you need, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, thanks for your suggestions!
@@ -1457,7 +1457,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { | |||
!Current.isOneOf(tok::colon, tok::comment)) { | |||
return ContinuationIndent; | |||
} | |||
if (Style.isCpp() && Current.is(tok::arrow) && | |||
if (Style.isCpp() && Current.is(TT_TrailingReturnArrow) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think you can drop the isCpp
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Technically also applies to attributes applied to the return type of the lambda, i.e.
...but currently at least no valid C++ attribute can go in that spot |
We can easily take care of it though. See #94560 (comment). |
Dear reviewers, is there anything else I need to do for this PR? If not, could you please assist me in merging it? Thank you! |
Closes #94181