Fixed #3678 (stlcstrReturn for classes which don't provide a std::string)

This commit is contained in:
Daniel Marjamäki 2012-11-29 07:10:56 +01:00
parent 6b40d93f6f
commit ba3833c692
2 changed files with 18 additions and 3 deletions

View File

@ -1196,7 +1196,7 @@ void CheckStl::string_c_str()
tok2 = tok2->previous();
if (tok2 && Token::simpleMatch(tok2->tokAt(-4), ". c_str ( )")) {
const Variable* var = symbolDatabase->getVariableFromVarId(tok2->tokAt(-5)->varId());
if (var && Token::Match(var->typeStartToken(), "const| std :: string|wstring"))
if (var && Token::Match(var->typeStartToken(), "const| std ::"))
string_c_strParam(tok, i->second);
}
}
@ -1238,8 +1238,17 @@ void CheckStl::string_c_str()
else if ((returnType == stdString || returnType == stdStringConstRef) && _settings->isEnabled("performance")) {
if (tok->str() == "return") {
const Token* tok2 = Token::findsimplematch(tok->next(), ";");
if (Token::simpleMatch(tok2->tokAt(-4), ". c_str ( )"))
string_c_strReturn(tok);
if (Token::simpleMatch(tok2->tokAt(-4), ". c_str ( )")) {
tok2 = tok2->tokAt(-5);
if (tok2->isName()) { // return var.c_str(); => check if var is a std type
const Variable* var = symbolDatabase->getVariableFromVarId(tok2->varId());
if (var && Token::Match(var->typeStartToken(), "const| std ::"))
string_c_strReturn(tok);
} else {
// TODO: determine if a error should be written or not
string_c_strReturn(tok);
}
}
}
}
}

View File

@ -1769,6 +1769,12 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("std::string get_msg() {\n" // #3678
" MyStringClass errmsg;\n"
" return errmsg.c_str();\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void Foo1(const std::string& str) {}\n"
"void Foo2(char* c, const std::string str) {}\n"