[clang-tools-extra] r282625 - [clang-tidy] fix false-positive for cppcoreguidelines-pro-type-member-init with in-class initializers

Matthias Gehre via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 28 13:06:18 PDT 2016


Author: mgehre
Date: Wed Sep 28 15:06:18 2016
New Revision: 282625

URL: http://llvm.org/viewvc/llvm-project?rev=282625&view=rev
Log:
[clang-tidy] fix false-positive for cppcoreguidelines-pro-type-member-init with in-class initializers

Summary:
This fixes https://llvm.org/bugs/show_bug.cgi?id=30487 where
```
warning: uninitialized record type: 's' [cppcoreguidelines-pro-type-member-init]
```
is emitted on
```
struct MyStruct
{
    int a = 5;
    int b = 7;
};

int main()
{
    MyStruct s;
}
```

Reviewers: alexfh, aaron.ballman

Subscribers: nemanjai, cfe-commits

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

Modified:
    clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp
    clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp

Modified: clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp?rev=282625&r1=282624&r2=282625&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp Wed Sep 28 15:06:18 2016
@@ -62,8 +62,10 @@ bool recordIsTriviallyDefaultConstructib
   if (ClassDecl->hasTrivialDefaultConstructor())
     return true;
 
-  // If all its fields are trivially constructible.
+  // If all its fields are trivially constructible and have no default initializers.
   for (const FieldDecl *Field : ClassDecl->fields()) {
+    if (Field->hasInClassInitializer())
+      return false;
     if (!isTriviallyDefaultConstructible(Field->getType(), Context))
       return false;
   }

Modified: clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp?rev=282625&r1=282624&r2=282625&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp Wed Sep 28 15:06:18 2016
@@ -73,6 +73,11 @@ struct NegativeInClassInitialized {
   NegativeInClassInitialized() {}
 };
 
+struct NegativeInClassInitializedDefaulted {
+  int F = 0;
+  NegativeInClassInitializedDefaulted() = default;
+};
+
 struct NegativeConstructorDelegated {
   int F;
 
@@ -367,3 +372,8 @@ class PositiveIndirectMember {
   PositiveIndirectMember() {}
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these fields: A
 };
+
+void Bug30487()
+{
+  NegativeInClassInitializedDefaulted s;
+}




More information about the cfe-commits mailing list