Skip to content

Commit 717aa82

Browse files
Ken-Patrickdanmar
authored andcommitted
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.
1 parent 3740c57 commit 717aa82

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ void CheckClass::initializationListUsage()
950950
break;
951951
if (Token::Match(tok, "try|do {"))
952952
break;
953-
if (!Token::Match(tok, "%var% =") || tok->strAt(-1) == "*")
953+
if (!Token::Match(tok, "%var% =") || tok->strAt(-1) == "*" || (tok->strAt(-1) == "." && tok->strAt(-2) != "this"))
954954
continue;
955955

956956
const Variable* var = tok->variable();

test/testclass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6692,6 +6692,15 @@ class TestClass : public TestFixture {
66926692
" }\n"
66936693
"};");
66946694
ASSERT_EQUALS("", errout.str());
6695+
6696+
// don't warn if some other instance's members are assigned to
6697+
checkInitializationListUsage("class C {\n"
6698+
"public:\n"
6699+
" C(C& c) : m_i(c.m_i) { c.m_i = (Foo)-1; }\n"
6700+
"private:\n"
6701+
" Foo m_i;\n"
6702+
"};");
6703+
ASSERT_EQUALS("", errout.str());
66956704
}
66966705

66976706

0 commit comments

Comments
 (0)