Skip to content

Spurious -Wuninitialized warning when referring to a previously initialized member of an aggregate inside an initializer list #156781

@akbyrd

Description

@akbyrd

This code:

struct Bar
{
	int get() { return 1; }
};

struct Foo
{
	Bar x;
	int y;
};

int main()
{
	Foo f = {
		.x = {},
		.y = f.x.get() + 1,
	};

	return f.y;
}

Causes this warning:

warning: variable 'f' is uninitialized when used within its own initialization [-Wuninitialized]

https://godbolt.org/z/6hhajcxYG

As far as I can tell, this is well defined and should not issue a warning. Foo is an aggregate and initialization expressions in a list initializer are sequenced. Neither GCC nor MSVC warn on this construct. Clang itself does not warn on other similar forms of this, such as:

struct Foo
{
	int x;
	int y;
};

int main()
{
	Foo f = {
		.x = {},
		.y = f.x + 1,
	};

	return f.y;
}

https://godbolt.org/z/od7MWYx1T

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions