Skip to content

Commit 0e7eb75

Browse files
committed
Dedicated ID, release notes
1 parent c8e0baf commit 0e7eb75

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

lib/checkclass.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ void CheckClass::constructors()
372372

373373
const Variable& var = *usage.var;
374374
if (diagVars.count(&var) == 0)
375-
uninitVarError(scope->bodyStart, false, FunctionType::eConstructor, var.scope()->className, var.name(), false, false);
375+
uninitVarError(scope->bodyStart, false, FunctionType::eConstructor, var.scope()->className, var.name(), false, false, true);
376376
}
377377
}
378378
}
@@ -1167,17 +1167,22 @@ void CheckClass::noExplicitConstructorError(const Token *tok, const std::string
11671167
reportError(tok, Severity::style, "noExplicitConstructor", "$symbol:" + classname + '\n' + message + '\n' + verbose, CWE398, Certainty::normal);
11681168
}
11691169

1170-
void CheckClass::uninitVarError(const Token *tok, bool isprivate, FunctionType functionType, const std::string &classname, const std::string &varname, bool derived, bool inconclusive)
1170+
void CheckClass::uninitVarError(const Token *tok, bool isprivate, FunctionType functionType, const std::string &classname, const std::string &varname, bool derived, bool inconclusive, bool noCtor)
11711171
{
1172-
std::string ctor;
1173-
if (functionType == FunctionType::eCopyConstructor)
1174-
ctor = "copy ";
1175-
else if (functionType == FunctionType::eMoveConstructor)
1176-
ctor = "move ";
1177-
std::string message("Member variable '$symbol' is not initialized in the " + ctor + "constructor.");
1172+
std::string message("Member variable '$symbol' ");
1173+
if (noCtor)
1174+
message += "has no initializer.";
1175+
else {
1176+
message += "is not initialized in the ";
1177+
if (functionType == FunctionType::eCopyConstructor)
1178+
message += "copy ";
1179+
else if (functionType == FunctionType::eMoveConstructor)
1180+
message += "move ";
1181+
message += "constructor.";
1182+
}
11781183
if (derived)
11791184
message += " Maybe it should be initialized directly in the class " + classname + "?";
1180-
std::string id = std::string("uninit") + (derived ? "Derived" : "") + "MemberVar" + (isprivate ? "Private" : "");
1185+
std::string id = std::string("uninit") + (derived ? "Derived" : "") + "MemberVar" + (isprivate ? "Private" : "") + (noCtor ? "NoCtor" : "");
11811186
const std::string verbose {message + " Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior."};
11821187
reportError(tok, Severity::warning, id, "$symbol:" + classname + "::" + varname + '\n' + message + '\n' + verbose, CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal);
11831188
}

lib/checkclass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class CPPCHECKLIB CheckClass : public Check {
155155
void noCopyConstructorError(const Scope *scope, bool isdefault, const Token *alloc, bool inconclusive);
156156
void noOperatorEqError(const Scope *scope, bool isdefault, const Token *alloc, bool inconclusive);
157157
void noDestructorError(const Scope *scope, bool isdefault, const Token *alloc);
158-
void uninitVarError(const Token *tok, bool isprivate, FunctionType functionType, const std::string &classname, const std::string &varname, bool derived, bool inconclusive);
158+
void uninitVarError(const Token *tok, bool isprivate, FunctionType functionType, const std::string &classname, const std::string &varname, bool derived, bool inconclusive, bool noCtor = false);
159159
void uninitVarError(const Token *tok, const std::string &classname, const std::string &varname);
160160
void missingMemberCopyError(const Token *tok, FunctionType functionType, const std::string& classname, const std::string& varname);
161161
void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive);

releasenotes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Major bug fixes & crashes:
66

77
New checks:
88
- MISRA C 2012 rule 10.3 now warns on assigning integer literals 0 and 1 to bool in C99 and later while preserving the existing C89 behavior.
9+
- uninitMemberVarNoCtor warns on user-defined types where some but not all members requiring initialization have in-class initializers.
910

1011
C/C++ support:
1112
-

test/testconstructors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ class TestConstructors : public TestFixture {
763763
check("struct S {\n" // #14546
764764
" int a = 0, b;\n"
765765
"};\n");
766-
ASSERT_EQUALS("[test.cpp:1:10]: (warning) Member variable 'S::b' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
766+
ASSERT_EQUALS("[test.cpp:1:10]: (warning) Member variable 'S::b' has no initializer. [uninitMemberVarNoCtor]\n", errout_str());
767767

768768
check("struct S {\n"
769769
" int a, b;\n"

0 commit comments

Comments
 (0)