Skip to content

Commit

Permalink
initializerList: argument name in error message
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jan 10, 2024
1 parent 5ef846a commit af04af8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2697,7 +2697,7 @@ void CheckClass::initializerListOrder()
// check for use of uninitialized arguments
for (const auto& arg : vars[j].initArgs)
if (vars[j].var->index() < arg->index())
initializerListError(vars[j].tok, vars[j].var->nameToken(), scope->className, vars[j].var->name(), /*isArgument*/ true);
initializerListError(vars[j].tok, vars[j].var->nameToken(), scope->className, vars[j].var->name(), arg->name());

// need at least 2 members to have out of order initialization
if (j == 0)
Expand All @@ -2712,12 +2712,12 @@ void CheckClass::initializerListOrder()
}
}

void CheckClass::initializerListError(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &varname, bool isArgument)
void CheckClass::initializerListError(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &varname, const std::string& argname)
{
std::list<const Token *> toks = { tok1, tok2 };
const std::string msg = isArgument ?
"Member variable '$symbol' uses an uninitialized argument due to the order of declarations." :
"Member variable '$symbol' is in the wrong place in the initializer list.";
const std::string msg = argname.empty() ?
"Member variable '$symbol' is in the wrong place in the initializer list." :
"Member variable '$symbol' uses an uninitialized argument '" + argname + "' due to the order of declarations.";
reportError(toks, Severity::style, "initializerList",
"$symbol:" + classname + "::" + varname + '\n' +
msg + '\n' +
Expand Down
2 changes: 1 addition & 1 deletion lib/checkclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class CPPCHECKLIB CheckClass : public Check {
void operatorEqToSelfError(const Token *tok);
void checkConstError(const Token *tok, const std::string &classname, const std::string &funcname, bool suggestStatic);
void checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname, bool suggestStatic);
void initializerListError(const Token *tok1,const Token *tok2, const std::string & classname, const std::string &varname, bool isArgument = false);
void initializerListError(const Token *tok1,const Token *tok2, const std::string & classname, const std::string &varname, const std::string& argname = {});
void suggestInitializationList(const Token *tok, const std::string& varname);
void selfInitializationError(const Token* tok, const std::string& varname);
void pureVirtualFunctionCallInConstructorError(const Function * scopeFunction, const std::list<const Token *> & tokStack, const std::string &purefuncname);
Expand Down
2 changes: 1 addition & 1 deletion test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7588,7 +7588,7 @@ class TestClass : public TestFixture {
" B b;\n"
" const A a;\n"
"};");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style, inconclusive) Member variable 'C::b' uses an uninitialized argument due to the order of declarations.\n",
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style, inconclusive) Member variable 'C::b' uses an uninitialized argument 'a' due to the order of declarations.\n",
errout.str());

checkInitializerListOrder("struct S {\n"
Expand Down

0 comments on commit af04af8

Please sign in to comment.