[clang-tidy] Fix the potential infinite loop in recordIsTriviallyDefaultConstructible.

Summary:
The recordIsTriviallyDefaultConstructible may cause an infinite loop when
running on an ill-formed decl.

Reviewers: gribozavr

Subscribers: nemanjai, xazax.hun, kbarton, cfe-commits

Tags: #clang

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

git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@370200 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/clang-tidy/utils/TypeTraits.cpp b/clang-tidy/utils/TypeTraits.cpp
index 954a288..9cc422a 100644
--- a/clang-tidy/utils/TypeTraits.cpp
+++ b/clang-tidy/utils/TypeTraits.cpp
@@ -54,6 +54,10 @@
   // Non-C++ records are always trivially constructible.
   if (!ClassDecl)
     return true;
+  // It is impossible to detemine whether an ill-formed decl is trivially
+  // constructible.
+  if (RecordDecl.isInvalidDecl())
+    return false;
   // A class with a user-provided default constructor is not trivially
   // constructible.
   if (ClassDecl->hasUserProvidedDefaultConstructor())
diff --git a/test/clang-tidy/cppcoreguidelines-pro-type-member-init-no-crash.cpp b/test/clang-tidy/cppcoreguidelines-pro-type-member-init-no-crash.cpp
new file mode 100644
index 0000000..300fff6
--- /dev/null
+++ b/test/clang-tidy/cppcoreguidelines-pro-type-member-init-no-crash.cpp
@@ -0,0 +1,7 @@
+// RUN: %check_clang_tidy -expect-clang-tidy-error %s cppcoreguidelines-pro-type-member-init %t
+
+struct X {
+  X x;
+  // CHECK-MESSAGES: :[[@LINE-1]]:5: error: field has incomplete type 'X' [clang-diagnostic-error]
+  int a = 10;
+};