Fixed useInitializationList false positives (#3988)

This commit is contained in:
PKEuS 2012-07-23 08:16:47 -07:00
parent ae6201d289
commit 5c0cab238f
2 changed files with 26 additions and 1 deletions

View File

@ -531,11 +531,17 @@ void CheckClass::initializationListUsage()
allowed = false; allowed = false;
break; 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) if (!allowed)
continue; 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()); suggestInitializationList(tok, tok->str());
} }
} }

View File

@ -5189,6 +5189,25 @@ private:
" Fred() { try { a = new int; } catch(...) {} }\n" " Fred() { try { a = new int; } catch(...) {} }\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); 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());
} }
}; };