cppcheck seems to incorrectly handle local structs/classes, w.r.t. default initialized members.
See following example:
bool testCppCheck() { struct TestLocalStruct { bool b {true}; bool get() const{ return b;} // CppCheck assumes b is always true }; TestLocalStruct s; bool b0 = s.get(); // b0 will be true (default initialized value of s.b) s.b = false; bool b1 = s.get(); // b1 will be false, but cppcheck says it will be 1 // cppcheck concludes, in the return statement below, that b0 and b1 // are always true and true will be returned. // But in fact, false will be returned (always), because b1 is false. return b0 && b1; }
will generate the following cppcheck output (using version 2.14.1):
Checking testCppCheck.cpp ... testCppCheck.cpp:22:10: style: Return value 'b0' is always true [knownConditionTrueFalse] return b0 && b1; ^ testCppCheck.cpp:5:13: note: Assignment 'b{true}', assigned value is 1 bool b {true}; ^ testCppCheck.cpp:14:18: note: Calling function 'get' returns 1 bool b0 = s.get(); // b0 will be true (default initialized value of s.b) ^ testCppCheck.cpp:14:18: note: Assignment 'b0=s.get()', assigned value is 1 bool b0 = s.get(); // b0 will be true (default initialized value of s.b) ^ testCppCheck.cpp:22:10: note: Return value 'b0' is always true return b0 && b1; ^ testCppCheck.cpp:22:16: style: Return value 'b1' is always true [knownConditionTrueFalse] return b0 && b1; ^ testCppCheck.cpp:5:13: note: Assignment 'b{true}', assigned value is 1 bool b {true}; ^ testCppCheck.cpp:17:18: note: Calling function 'get' returns 1 bool b1 = s.get(); // b1 will be false, but cppcheck says it will be 1 ^ testCppCheck.cpp:17:18: note: Assignment 'b1=s.get()', assigned value is 1 bool b1 = s.get(); // b1 will be false, but cppcheck says it will be 1 ^ testCppCheck.cpp:22:16: note: Return value 'b1' is always true return b0 && b1; ^
This will not be reported if the member TestLocalStruct::b is defined after method TestLocalStruct::get().
TestLocalStruct::b
TestLocalStruct::get()
Can this issue be followed up (create a bugreport), if it is not already?
Thanx, Istvan
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/13208
Log in to post a comment.
cppcheck seems to incorrectly handle local structs/classes, w.r.t. default initialized members.
See following example:
will generate the following cppcheck output (using version 2.14.1):
This will not be reported if the member
TestLocalStruct::b
is defined after methodTestLocalStruct::get()
.Can this issue be followed up (create a bugreport), if it is not already?
Thanx,
Istvan
Last edit: Istvan Eperjesy 2024-10-11
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/13208