[clang] [clang-format] fix incorrectly indents lambda trailing return (PR #94560)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 5 19:46:05 PDT 2024


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/94560

close: #94181

>From 624e74b3066930a5a2bb15d6c0b2ddb4f94d3b79 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Thu, 6 Jun 2024 10:44:57 +0800
Subject: [PATCH] fix incorrectly indents lambda trailing return

---
 clang/lib/Format/ContinuationIndenter.cpp |  5 +++++
 clang/unittests/Format/FormatTest.cpp     | 25 +++++++++++++++++++++++
 2 files changed, 30 insertions(+)

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"



More information about the cfe-commits mailing list