76 lines · plain
1.. title:: clang-tidy - modernize-use-designated-initializers2 3modernize-use-designated-initializers4=====================================5 6Finds initializer lists for aggregate types which could be written as7designated initializers instead.8 9With plain initializer lists, it is very easy to introduce bugs when adding new10fields in the middle of a struct or class type. The same confusion might arise11when changing the order of fields.12 13C++20 supports the designated initializer syntax for aggregate types. By14applying it, we can always be sure that aggregates are constructed correctly,15because every variable being initialized is referenced by its name.16 17Example:18 19.. code-block::20 21 struct S { int i, j; };22 23is an aggregate type that should be initialized as24 25.. code-block::26 27 S s{.i = 1, .j = 2};28 29instead of30 31.. code-block::32 33 S s{1, 2};34 35which could easily become an issue when ``i`` and ``j`` are swapped in the36declaration of ``S``.37 38Even when compiling in a language version older than C++20, depending on your39compiler, designated initializers are potentially supported. Therefore, the40check is by default restricted to C99/C++20 and above. Check out the options41`-Wc99-designator` to get support for mixed designators in initializer list42in C and `-Wc++20-designator` for support of designated initializers in older43C++ language modes.44 45Options46-------47 48.. option:: IgnoreMacros49 50 The value `false` specifies that components of initializer lists expanded from51 macros are not checked. The default value is `true`.52 53.. option:: IgnoreSingleElementAggregates54 55 The value `false` specifies that even initializers for aggregate types with56 only a single element should be checked. The default value is `true`.57 ``std::array`` initializations are always excluded, as the type is a58 standard library abstraction and not intended to be initialized with59 designated initializers.60 61.. option:: RestrictToPODTypes62 63 The value `true` specifies that only Plain Old Data (POD) types shall be64 checked. This makes the check applicable to even older C++ standards. The65 default value is `false`.66 67.. option:: StrictCStandardCompliance68 69 When set to `false`, the check will not restrict itself to C99 and above.70 The default value is `true`.71 72.. option:: StrictCppStandardCompliance73 74 When set to `false`, the check will not restrict itself to C++20 and above.75 The default value is `true`.76