diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 049048e97..27ed19f38 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -657,7 +657,7 @@ void CheckClass::initializationListUsage() break; if (tok->varId() && Token::Match(tok, "%var% = %any%")) { const Variable* var = symbolDatabase->getVariableFromVarId(tok->varId()); - if (var && var->scope() == owner) { + if (var && var->scope() == owner && !var->isStatic()) { bool allowed = true; for (const Token* tok2 = tok->tokAt(2); tok2->str() != ";"; tok2 = tok2->next()) { if (tok2->varId()) { diff --git a/test/testclass.cpp b/test/testclass.cpp index d94050eff..72392f3d8 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -5399,6 +5399,12 @@ private: " 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()); + + 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."