Fixed false positive #4032

This commit is contained in:
PKEuS 2012-08-10 06:26:07 -07:00
parent 0254344df5
commit 70de691551
2 changed files with 15 additions and 4 deletions

View File

@ -1332,6 +1332,8 @@ void CheckStl::uselessCalls()
if (!performance && !style)
return;
const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase();
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->varId() && Token::Match(tok, "%var% . compare|find|rfind|find_first_not_of|find_first_of|find_last_not_of|find_last_of ( %var% [,)]") &&
tok->varId() == tok->tokAt(4)->varId() && style) {
@ -1342,9 +1344,10 @@ void CheckStl::uselessCalls()
} else if (Token::simpleMatch(tok, ". substr (") && performance) {
if (Token::Match(tok->tokAt(3), "0| )"))
uselessCallsSubstrError(tok, false);
else if (tok->strAt(3) == "0" && tok->linkAt(2)->strAt(-1) == "npos")
uselessCallsSubstrError(tok, false);
else if (Token::simpleMatch(tok->linkAt(2)->tokAt(-2), ", 0 )"))
else if (tok->strAt(3) == "0" && tok->linkAt(2)->strAt(-1) == "npos") {
if (!symbolDatabase->getVariableFromVarId(tok->linkAt(2)->previous()->varId())) // Make sure that its no variable
uselessCallsSubstrError(tok, false);
} else if (Token::simpleMatch(tok->linkAt(2)->tokAt(-2), ", 0 )"))
uselessCallsSubstrError(tok, true);
} else if (Token::Match(tok, "[{}:;] %var% . empty ( ) ;") && style)
uselessCallsEmptyError(tok->next());

View File

@ -1872,7 +1872,15 @@ private:
" return v.empty();\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Useless call of function 'empty()'. Did you intend to call 'clear()' instead?\n", errout.str());
check("void f() {\n" // #4032
" const std::string greeting(\"Hello World !!!\");\n"
" const std::string::size_type npos = greeting.rfind(\" \");\n"
" if (npos != std::string::npos)\n"
" std::cout << greeting.substr(0, npos) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
};
};
REGISTER_TEST(TestStl)