Skip to content

[clang-tidy] Create check to enforce usage of designated initializers #77618

Closed
@PiotrZSL

Description

@PiotrZSL

Info: https://en.cppreference.com/w/cpp/language/aggregate_initialization

Code like this:

struct A
{
   int a;
   int b;
};

void test()
{
   A v{10, 20};
}
// should be changed into:
void test()
{
   A v{.a = 10, .b=20};
}

Rationale: It's very easy to introduce bug when adding some field in middle of struct, or changing order of fields, with this we can always be sure that such objects are constructed correctly.

Check name: modernize-use-designated-initializers.
Check should enforce that all aggregates should be initialized with designated initializers.
There can be config to ignore aggregates that have only 1 argument.
There can be config to reduce check to only trivial types, in such case it could be used on older standards with compiler extensions.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions