diff --git a/lib/Format/NamespaceEndCommentsFixer.cpp b/lib/Format/NamespaceEndCommentsFixer.cpp
index af751100ece03763163d48d36d8c07c43881a0ec..88cf123c189909a05b6bd0954b12dec38819a03c 100644
--- a/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -133,7 +133,7 @@ tooling::Replacements NamespaceEndCommentsFixer::analyze(
     // Detect "(inline)? namespace" in the beginning of a line.
     if (NamespaceTok->is(tok::kw_inline))
       NamespaceTok = NamespaceTok->getNextNonComment();
-    if (NamespaceTok->isNot(tok::kw_namespace))
+    if (!NamespaceTok || NamespaceTok->isNot(tok::kw_namespace))
       continue;
     FormatToken *RBraceTok = EndLine->First;
     if (RBraceTok->Finalized)
diff --git a/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/unittests/Format/NamespaceEndCommentsFixerTest.cpp
index 48ecdb038e9a775064078d3bf16beffca10213ad..912638f456525241e8beb349e452b61743d13fa0 100644
--- a/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ b/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -582,6 +582,21 @@ TEST_F(NamespaceEndCommentsFixerTest,
                                     "} // namespace\n"
                                     "}"));
 }
+
+TEST_F(NamespaceEndCommentsFixerTest, HandlesInlineAtEndOfLine_PR32438) {
+  EXPECT_EQ("template <int> struct a {};\n"
+            "struct a<bool{}> b() {\n"
+            "}\n"
+            "#define c inline\n"
+            "void d() {\n"
+            "}\n",
+            fixNamespaceEndComments("template <int> struct a {};\n"
+                                    "struct a<bool{}> b() {\n"
+                                    "}\n"
+                                    "#define c inline\n"
+                                    "void d() {\n"
+                                    "}\n"));
+}
 } // end namespace
 } // end namespace format
 } // end namespace clang