namespaceconstParameterFalsePositiveWithUnion{unionUnionToSetStructData{struct{unsignedlongone:8;unsignedlongtwo:13;unsignedlongthree:11;unsignedlongfour:3;unsignedlongfive:5;}FourByteStruct;unsignedcharTheFilledData[4];//intentionallysamesizeasthestructinbytes};boolGetData(UnionToSetStructData&data)//dataisanin/outparameter{UnionToSetStructDataaUnion;//commentoutallthesetsto0andyougetunusedStructMemberwarningsforeachbut//pertherulesofunionsthatdoesn't make sense because TheFilledData is last active//Thislookslikehttps://trac.cppcheck.net/ticket/10293butIamusing2.5soitdoesn't seem like it is fixed yetaUnion.FourByteStruct.one=0;aUnion.FourByteStruct.two=0;aUnion.FourByteStruct.three=0;aUnion.FourByteStruct.four=0;aUnion.FourByteStruct.five=0;memcpy(aUnion.TheFilledData,data.TheFilledData,sizeof(data));//justsoaUnion.TheDataisconsideredactiveintheunionmemcpy(data.TheFilledData,aUnion.TheFilledData,sizeof(data));//examplecode,wewouldn't restore this normallyreturntrue;}}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
constParameter - Parameter 'data' can be declared with const
https://en.cppreference.com/w/cpp/language/union