Don't suggest using initialization list for static variables (#4332)
This commit is contained in:
parent
e44ee280bc
commit
b0c1c2c819
|
@ -657,7 +657,7 @@ void CheckClass::initializationListUsage()
|
||||||
break;
|
break;
|
||||||
if (tok->varId() && Token::Match(tok, "%var% = %any%")) {
|
if (tok->varId() && Token::Match(tok, "%var% = %any%")) {
|
||||||
const Variable* var = symbolDatabase->getVariableFromVarId(tok->varId());
|
const Variable* var = symbolDatabase->getVariableFromVarId(tok->varId());
|
||||||
if (var && var->scope() == owner) {
|
if (var && var->scope() == owner && !var->isStatic()) {
|
||||||
bool allowed = true;
|
bool allowed = true;
|
||||||
for (const Token* tok2 = tok->tokAt(2); tok2->str() != ";"; tok2 = tok2->next()) {
|
for (const Token* tok2 = tok->tokAt(2); tok2->str() != ";"; tok2 = tok2->next()) {
|
||||||
if (tok2->varId()) {
|
if (tok2->varId()) {
|
||||||
|
|
|
@ -5399,6 +5399,12 @@ private:
|
||||||
" Fred() { a = foo(); }\n"
|
" Fred() { a = foo(); }\n"
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (performance) Variable 'a' is assigned in constructor body. Consider performing initialization in initialization list.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (performance) Variable 'a' is assigned in constructor body. Consider performing initialization in initialization list.\n", errout.str());
|
||||||
|
|
||||||
|
checkInitializationListUsage("class Fred {\n" // #4332
|
||||||
|
" static std::string s;\n"
|
||||||
|
" Fred() { s = \"foo\"; }\n"
|
||||||
|
"};");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."
|
// ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."
|
||||||
|
|
Loading…
Reference in New Issue