[clang-tools-extra] [clang-tidy] Add new check `modernize-use-designated-initializers` (PR #80541)
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 3 07:18:06 PST 2024
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>,
Danny =?utf-8?q?Mösch?= <danny.moesch at icloud.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/80541 at github.com>
================
@@ -0,0 +1,48 @@
+// RUN: %check_clang_tidy -std=c++20 %s modernize-use-designated-initializers %t
+// RUN: %check_clang_tidy -check-suffixes=,SINGLE-ELEMENT -std=c++20 %s modernize-use-designated-initializers %t \
+// RUN: -- -config="{CheckOptions: [{key: modernize-use-designated-initializers.IgnoreSingleElementAggregates, value: false}]}" \
+// RUN: --
+
+struct S1 {};
+
+S1 s11{};
+S1 s12 = {};
+S1 s13();
+S1 s14;
+
+struct S2 { int i, j; };
+
+S2 s21{.i=1, .j =2};
+
+S2 s22 = {1, 2};
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use designated initializer list [modernize-use-designated-initializers]
+
+S2 s23{1};
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use designated initializer list [modernize-use-designated-initializers]
+
+S2 s24{.i = 1};
+
+S2 s25 = {.i=1, 2};
+// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use designated init expression [modernize-use-designated-initializers]
+
+class S3 {
+ public:
+ S2 s2;
+ double d;
+};
+
+S3 s31 = {.s2 = 1, 2, 3.1};
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use designated init expression [modernize-use-designated-initializers]
+// CHECK-MESSAGES: :[[@LINE-2]]:23: warning: use designated init expression [modernize-use-designated-initializers]
+
+S3 s32 = {{.i = 1, 2}};
+// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use designated initializer list [modernize-use-designated-initializers]
+// CHECK-MESSAGES: :[[@LINE-2]]:20: warning: use designated init expression [modernize-use-designated-initializers]
+
+struct S4 {
+ double d;
+ private: static int i;
+};
+
+S4 s41 {2.2};
+// CHECK-MESSAGES-SINGLE-ELEMENT: :[[@LINE-1]]:8: warning: use designated initializer list [modernize-use-designated-initializers]
----------------
PiotrZSL wrote:
Add tests with base class like:
```
template<typename T>
T init()
{
return {10, 11};
}
struct A
{
int x,y;
};
struct B : A {
int a,b;
};
void test()
{
// init<A>();
init<B>();
}
```
problem is that without designated initializers:
```
CompoundStmt <line:4:1, line:6:1>
| `-ReturnStmt <line:5:4, col:18>
| `-InitListExpr <col:11, col:18> 'B':'B'
| |-InitListExpr <col:12, col:16> 'A':'A'
| | |-IntegerLiteral <col:12> 'int' 10
| | `-IntegerLiteral <col:16> 'int' 11
| |-ImplicitValueInitExpr 'int'
| `-ImplicitValueInitExpr 'int'
```
actually base class is initialized, but with designated initializers:
```
template<typename T>
T init()
{
return {.a=10, .b=11};
}
`-CompoundStmt <line:4:1, line:6:1>
| `-ReturnStmt <line:5:4, col:24>
| `-InitListExpr <col:11, col:24> 'B':'B'
| |-InitListExpr <col:24> 'A':'A'
| | |-ImplicitValueInitExpr 'int'
| | `-ImplicitValueInitExpr 'int'
| |-IntegerLiteral <col:15> 'int' 10
| `-IntegerLiteral <col:22> 'int' 11
```
its ta delivered class that is initialized, to init base class double {} are needed:
```
template<typename T>
T init()
{
return {{.x=10, .y=11}, .a=12, .b=14};
}
but with this there is a way to properly initialize both but there is compiler warning triggered due to mixing.
```
Would be very good to exclude classes that got base class with fields in first version to avoid such problems.
https://github.com/llvm/llvm-project/pull/80541
More information about the cfe-commits
mailing list