From 520cca87e38185872212849562951906be5068e9 Mon Sep 17 00:00:00 2001
From: Daniel Jasper <djasper@google.com>
Date: Fri, 6 Sep 2013 21:25:51 +0000
Subject: [PATCH] clang-format: Don't confuse operator[] with lambdas.

Before:
  double &operator[](int i) { return 0; } int i;

After:
  double &operator[](int i) { return 0; }
  int i;

This fixes llvm.org/PR17134.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190207 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Format/UnwrappedLineParser.cpp | 3 ++-
 unittests/Format/FormatTest.cpp    | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp
index 814ccf42a88..521df8f0285 100644
--- a/lib/Format/UnwrappedLineParser.cpp
+++ b/lib/Format/UnwrappedLineParser.cpp
@@ -676,7 +676,8 @@ void UnwrappedLineParser::parseStructuralElement() {
 void UnwrappedLineParser::tryToParseLambda() {
   // FIXME: This is a dirty way to access the previous token. Find a better
   // solution.
-  if (!Line->Tokens.empty() && Line->Tokens.back().Tok->is(tok::identifier)) {
+  if (!Line->Tokens.empty() &&
+      Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator)) {
     nextToken();
     return;
   }
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 57aa9ca4014..ae35cccc434 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -6399,6 +6399,8 @@ TEST_F(FormatTest, FormatsLambdas) {
 
   // Not lambdas.
   verifyFormat("constexpr char hello[]{ \"hello\" };");
+  verifyFormat("double &operator[](int i) { return 0; }\n"
+               "int i;");
 }
 
 TEST_F(FormatTest, FormatsBlocks) {
-- 
GitLab