From 717aa826d8c98dab533b7c2166eb412b1422b14d Mon Sep 17 00:00:00 2001 From: Ken-Patrick Date: Sat, 31 Aug 2019 12:27:07 +0200 Subject: [PATCH] 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. --- lib/checkclass.cpp | 2 +- test/testclass.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index ead95eb4d..674deb7c2 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -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(); diff --git a/test/testclass.cpp b/test/testclass.cpp index d9b4111ba..48a638240 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -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()); }