From 5c0cab238f5fec6d535e8460a4b9fd3f7484c474 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 23 Jul 2012 08:16:47 -0700 Subject: [PATCH] Fixed useInitializationList false positives (#3988) --- lib/checkclass.cpp | 8 +++++++- test/testclass.cpp | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index c1809395b..a281cbe42 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -531,11 +531,17 @@ void CheckClass::initializationListUsage() allowed = false; break; } + } else if (tok2->str() == "this") { // 'this' instance is not completely constructed in initialization list + allowed = false; + break; + } else if (Token::Match(tok2, "%var% (") && tok2->strAt(-1) != "." && isMemberFunc(owner, tok2)) { // Member function called? + allowed = false; + break; } } if (!allowed) continue; - if (!var->isPointer() && (var->type() || Token::Match(var->typeStartToken(), "std :: string|wstring") || Token::Match(var->typeStartToken(), "std :: %type% <") || symbolDatabase->isClassOrStruct(var->typeStartToken()->str()))) + if (!var->isPointer() && (var->type() || Token::Match(var->typeStartToken(), "std :: string|wstring !!::") || (Token::Match(var->typeStartToken(), "std :: %type% <") && !Token::simpleMatch(var->typeStartToken()->linkAt(3), "> ::")) || symbolDatabase->isClassOrStruct(var->typeStartToken()->str()))) suggestInitializationList(tok, tok->str()); } } diff --git a/test/testclass.cpp b/test/testclass.cpp index 2fd7f719c..c0927ff69 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -5189,6 +5189,25 @@ private: " Fred() { try { a = new int; } catch(...) {} }\n" "};"); ASSERT_EQUALS("", errout.str()); + + checkInitializationListUsage("class Fred {\n" + " std::string s;\n" + " Fred() { s = toString((size_t)this); }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + checkInitializationListUsage("class Fred {\n" + " std::string a;\n" + " std::string foo();\n" + " Fred() { a = foo(); }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + checkInitializationListUsage("class Fred {\n" + " std::string a;\n" + " Fred() { a = foo(); }\n" + "};"); + ASSERT_EQUALS("[test.cpp:3]: (performance) Variable 'a' is assigned in constructor body. Consider to perform initalization in initialization list.\n", errout.str()); } };