uninitMemberVarNoCtor asks for a default member initializer on members of a constructor-less aggregate. Two kinds of member cannot take one:
Bit-fields under --std=c++14/c++17: default member initializers
for bit-fields only became valid in C++20. Cppcheck warns even with --std=c++14, so the suggested fix does not compile.
Flexible array members (char Data[];, GNU/C99 extension common in
wire/IPC headers): they have no size, so an initializer is impossible in
any standard.
A user-written constructor is not a practical alternative for (1) because it removes aggregate initialization, which these wire-layout structs rely on.
bitfield.cpp:5:17: warning: Member variable 'Wire::Mode' has no initializer. [uninitMemberVarNoCtor]
bitfield.cpp:6:17: warning: Member variable 'Wire::Reserved' has no initializer. [uninitMemberVarNoCtor]
The suggested fix is rejected by the compiler in C++14:
structW{unsignedcharM:4=0;};
$ g++ -std=c++14 -pedantic-errors -fsyntax-only bfinit.cpp
bfinit.cpp:1:34: error: lvalue required as left operand of assignment
Expected:
No uninitMemberVarNoCtor for bit-fields when --std is older than
C++20 (or at least a message that does not recommend an ill-formed fix).
uninitMemberVarNoCtorasks for a default member initializer on members of a constructor-less aggregate. Two kinds of member cannot take one:--std=c++14/c++17: default member initializersfor bit-fields only became valid in C++20. Cppcheck warns even with
--std=c++14, so the suggested fix does not compile.char Data[];, GNU/C99 extension common inwire/IPC headers): they have no size, so an initializer is impossible in
any standard.
A user-written constructor is not a practical alternative for (1) because it removes aggregate initialization, which these wire-layout structs rely on.
Reproduction 1: bit-fields with
--std=c++14bitfield.cpp:Actual:
The suggested fix is rejected by the compiler in C++14:
Expected:
No
uninitMemberVarNoCtorfor bit-fields when--stdis older thanC++20 (or at least a message that does not recommend an ill-formed fix).
Reproduction 2: flexible array member
flex.cpp:Actual:
Expected:
No warning, a flexible array member has no storage in the struct itself and cannot have an initializer.
Version
Cppcheck 2.22.0(Linux)Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15177