Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/modernize/use-designated-initializers.rst

raw
1 .. title:: clang-tidy - modernize-use-designated-initializers
2
3 modernize-use-designated-initializers
4 =====================================
5
6 Finds initializer lists for aggregate types which could be written as designated
7 initializers instead.
8
9 With plain initializer lists, it is very easy to introduce bugs when adding new
10 fields in the middle of a struct or class type. The same confusion might arise
11 when changing the order of fields.
12
13 C++20 supports the designated initializer syntax for aggregate types. By
14 applying it, we can always be sure that aggregates are constructed correctly,
15 because every variable being initialized is referenced by its name.
16
17 Example:
18
19 .. code-block::
20
21 struct S { int i, j; };
22
23 is an aggregate type that should be initialized as
24
25 .. code-block::
26
27 S s{.i = 1, .j = 2};
28
29 instead of
30
31 .. code-block::
32
33 S s{1, 2};
34
35 which could easily become an issue when ``i`` and ``j`` are swapped in the
36 declaration of ``S``.
37
38 Even when compiling in a language version older than C++20, depending on your
39 compiler, designated initializers are potentially supported. Therefore, the
40 check is by default restricted to C99/C++20 and above. Check out the options
41 ``-Wc99-designator`` to get support for mixed designators in initializer list in
42 C and ``-Wc++20-designator`` for support of designated initializers in older C++
43 language modes.
44
45 Options
46 -------
47
48 .. option:: IgnoreMacros
49
50 The value `false` specifies that components of initializer lists expanded from
51 macros are not checked. The default value is `true`.
52
53 .. option:: IgnoreSingleElementAggregates
54
55 The value `false` specifies that even initializers for aggregate types with
56 only a single element should be checked. The default value is `true`.
57
58 .. option:: RestrictToPODTypes
59
60 The value `true` specifies that only Plain Old Data (POD) types shall be
61 checked. This makes the check applicable to even older C++ standards. The
62 default value is `false`.
63
64 .. option:: StrictCStandardCompliance
65
66 When set to `false`, the check will not restrict itself to C99 and above.
67 The default value is `true`.
68
69 .. option:: StrictCppStandardCompliance
70
71 When set to `false`, the check will not restrict itself to C++20 and above.
72 The default value is `true`.