[clang-format] Fixed edge-case with SpacesInSquareBrackets with trailing bare "&...
authorMitchell Balan <mitchell@stellarscience.com>
Thu, 14 Nov 2019 18:18:34 +0000 (13:18 -0500)
committerMitchell Balan <mitchell@stellarscience.com>
Thu, 14 Nov 2019 18:24:50 +0000 (13:24 -0500)
Summary:
Lambda captures allow for a lone `&` capture, so `&]` needs to be properly handled.

`int foo = [& ]() {}` is fixed to give `int foo = [ & ]() {}`

Reviewers: MyDeveloperDay

Reviewed by: MyDeveloperDay

Subscribers: cfe-commits

Tags: #clang, #clang-format

Differential Revision: https://reviews.llvm.org/D70249

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

index d3c9dd56f97c10a5152296664ef2a1a6b3035da7..c5ed49e9a409231959b9b0195fc23109e88258f3 100644 (file)
@@ -2996,14 +2996,18 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     return true;
   }
   if (Left.is(TT_UnaryOperator)) {
-    // The alternative operators for ~ and ! are "compl" and "not".
-    // If they are used instead, we do not want to combine them with
-    // the token to the right, unless that is a left paren.
     if (!Right.is(tok::l_paren)) {
+      // The alternative operators for ~ and ! are "compl" and "not".
+      // If they are used instead, we do not want to combine them with
+      // the token to the right, unless that is a left paren.
       if (Left.is(tok::exclaim) && Left.TokenText == "not")
         return true;
       if (Left.is(tok::tilde) && Left.TokenText == "compl")
         return true;
+      // Lambda captures allow for a lone &, so "&]" needs to be properly
+      // handled.
+      if (Left.is(tok::amp) && Right.is(tok::r_square))
+        return Style.SpacesInSquareBrackets;
     }
     return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
            Right.is(TT_BinaryOperator);
index 01154e2c2f40e8b4b1509dada4c545b0e9c7ca53..b8a73621c77909b6bf4453413b79945af22ce332 100644 (file)
@@ -10638,6 +10638,7 @@ TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
   verifyFormat("return [ i, args... ] {};", Spaces);
   verifyFormat("int foo = [ &bar ]() {};", Spaces);
   verifyFormat("int foo = [ = ]() {};", Spaces);
+  verifyFormat("int foo = [ & ]() {};", Spaces);
   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
 }