Fix false positive in initializationListUsage (#2128)

https://sourceforge.net/p/cppcheck/discussion/general/thread/d5b690ef19/
Check that we warn only about using the initializer list when we assign
the object being constructed.
This commit is contained in:
Ken-Patrick 2019-08-31 12:27:07 +02:00 committed by Daniel Marjamäki
parent 3740c57160
commit 717aa826d8
2 changed files with 10 additions and 1 deletions

View File

@ -950,7 +950,7 @@ void CheckClass::initializationListUsage()
break;
if (Token::Match(tok, "try|do {"))
break;
if (!Token::Match(tok, "%var% =") || tok->strAt(-1) == "*")
if (!Token::Match(tok, "%var% =") || tok->strAt(-1) == "*" || (tok->strAt(-1) == "." && tok->strAt(-2) != "this"))
continue;
const Variable* var = tok->variable();

View File

@ -6692,6 +6692,15 @@ private:
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
// don't warn if some other instance's members are assigned to
checkInitializationListUsage("class C {\n"
"public:\n"
" C(C& c) : m_i(c.m_i) { c.m_i = (Foo)-1; }\n"
"private:\n"
" Foo m_i;\n"
"};");
ASSERT_EQUALS("", errout.str());
}