Improve detection of trailing return types.

Trailing return types can only occur in declaration contexts.

Before:
  void f() { auto a = b -> c(); }

After:
  void f() { auto a = b->c(); }

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@186087 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 1e4de6e..1420644 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -607,7 +607,8 @@
         NameFound = true;
       } else if (Current.is(tok::kw_auto)) {
         AutoFound = true;
-      } else if (Current.is(tok::arrow) && AutoFound) {
+      } else if (Current.is(tok::arrow) && AutoFound &&
+                 Line.MustBeDeclaration) {
         Current.Type = TT_TrailingReturnArrow;
       } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
         Current.Type =
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 42cd89c..37ac1a2 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -2476,6 +2476,9 @@
   verifyFormat("template <size_t Order, typename T>\n"
                "auto load_img(const std::string &filename)\n"
                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
+
+  // Not trailing return types.
+  verifyFormat("void f() { auto a = b->c(); }");
 }
 
 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {