[clang-tools-extra] Adding an initial version of the "Initialized Class Members" checker. (PR #65189)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 1 16:44:32 PDT 2023
================
@@ -0,0 +1,56 @@
+// RUN: %check_clang_tidy %s google-cpp-init-class-members %t
+
+class PositiveDefaultedDefaultConstructor {
+public:
+ PositiveDefaultedDefaultConstructor() = default;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor should initialize these fields: X
+
+private:
+ int X;
+};
+
+class PositiveDefaultedDefaultConstructorWithInitializedField {
+public:
+ PositiveDefaultedDefaultConstructorWithInitializedField() = default;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor should initialize these fields: X
+
+private:
+ int X;
+ int Y = 4; // no-warning
+};
+
+class Helper {
+ public:
+ Helper(int x) : X(x) {}
+
+ private:
+ int X;
+};
+
+class PositiveDefaultedConstructorObjectAndPrimitive {
+ public:
+ PositiveDefaultedConstructorObjectAndPrimitive() = default;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor should initialize these fields: Y
+
+ Helper* GetHelper() { return &X; }
+
+ void SetY(bool enabled) { Y = enabled; }
+
+ bool IsY() { return Y; }
+
+ private:
+ Helper X;
+ bool Y;
+};
+
+struct PositiveStruct {
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: these fields should be initialized: X, Y
+ int X;
+ int Y;
+};
+
+struct PositiveStructWithInitializedField {
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: these fields should be initialized: Y
+ int X = 3; // no-warning
+ int Y;
+};
----------------
Xazax-hun wrote:
I suggest you to add some TODO messages to show which other cases are you planning to cover.
Some ideas:
* Inheritance
* Struct with anonymous members
* Struct with anonymous union members
* Unnamed structs
* Nested structs
Sometimes you might need to make a judgement call in this check.
For example, what are the plans to handle cases like:
```
struct A {
struct {
int a;
int b;
} unnamed{};
};
```
https://github.com/llvm/llvm-project/pull/65189
More information about the cfe-commits
mailing list