Fixed #5677 (Fix overzealous substr() warning)
This commit is contained in:
parent
dbf2c44a81
commit
b4b340b7be
|
@ -1371,6 +1371,10 @@ void CheckStl::autoPointerArrayError(const Token *tok)
|
||||||
|
|
||||||
void CheckStl::uselessCalls()
|
void CheckStl::uselessCalls()
|
||||||
{
|
{
|
||||||
|
// THIS ARRAY MUST BE ORDERED ALPHABETICALLY
|
||||||
|
static const char* const stl_string[] = {
|
||||||
|
"string", "u16string", "u32string", "wstring"
|
||||||
|
};
|
||||||
// THIS ARRAY MUST BE ORDERED ALPHABETICALLY
|
// THIS ARRAY MUST BE ORDERED ALPHABETICALLY
|
||||||
static const char* const stl_containers_with_empty_and_clear[] = {
|
static const char* const stl_containers_with_empty_and_clear[] = {
|
||||||
"deque", "forward_list", "list",
|
"deque", "forward_list", "list",
|
||||||
|
@ -1395,13 +1399,14 @@ void CheckStl::uselessCalls()
|
||||||
} else if (tok->varId() && Token::Match(tok, "%var% . swap ( %var% )") &&
|
} else if (tok->varId() && Token::Match(tok, "%var% . swap ( %var% )") &&
|
||||||
tok->varId() == tok->tokAt(4)->varId() && performance) {
|
tok->varId() == tok->tokAt(4)->varId() && performance) {
|
||||||
uselessCallsSwapError(tok, tok->str());
|
uselessCallsSwapError(tok, tok->str());
|
||||||
} else if (Token::simpleMatch(tok, ". substr (") && performance) {
|
} else if (Token::Match(tok, "%var% . substr (") && performance &&
|
||||||
if (Token::Match(tok->tokAt(3), "0| )"))
|
tok->variable() && tok->variable()->isStlType(stl_string)) {
|
||||||
|
if (Token::Match(tok->tokAt(4), "0| )"))
|
||||||
uselessCallsSubstrError(tok, false);
|
uselessCallsSubstrError(tok, false);
|
||||||
else if (tok->strAt(3) == "0" && tok->linkAt(2)->strAt(-1) == "npos") {
|
else if (tok->strAt(4) == "0" && tok->linkAt(3)->strAt(-1) == "npos") {
|
||||||
if (!tok->linkAt(2)->previous()->variable()) // Make sure that its no variable
|
if (!tok->linkAt(3)->previous()->variable()) // Make sure that its no variable
|
||||||
uselessCallsSubstrError(tok, false);
|
uselessCallsSubstrError(tok, false);
|
||||||
} else if (Token::simpleMatch(tok->linkAt(2)->tokAt(-2), ", 0 )"))
|
} else if (Token::simpleMatch(tok->linkAt(3)->tokAt(-2), ", 0 )"))
|
||||||
uselessCallsSubstrError(tok, true);
|
uselessCallsSubstrError(tok, true);
|
||||||
} else if (Token::Match(tok, "[{};] %var% . empty ( ) ;") && warning &&
|
} else if (Token::Match(tok, "[{};] %var% . empty ( ) ;") && warning &&
|
||||||
tok->next()->variable() && tok->next()->variable()->isStlType(stl_containers_with_empty_and_clear))
|
tok->next()->variable() && tok->next()->variable()->isStlType(stl_containers_with_empty_and_clear))
|
||||||
|
|
|
@ -2260,7 +2260,7 @@ private:
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int x=1;\n"
|
" int x=1;\n"
|
||||||
" string s1, s2;\n"
|
" std::string s1, s2;\n"
|
||||||
" s1 = s1.substr();\n"
|
" s1 = s1.substr();\n"
|
||||||
" s2 = s1.substr(x);\n"
|
" s2 = s1.substr(x);\n"
|
||||||
" s1 = s2.substr(0, x);\n"
|
" s1 = s2.substr(0, x);\n"
|
||||||
|
@ -2273,6 +2273,18 @@ private:
|
||||||
"the object. Use operator= instead.\n"
|
"the object. Use operator= instead.\n"
|
||||||
"[test.cpp:9]: (performance) Ineffective call of function \'substr\' because it returns an empty string.\n", errout.str());
|
"[test.cpp:9]: (performance) Ineffective call of function \'substr\' because it returns an empty string.\n", errout.str());
|
||||||
|
|
||||||
|
check("void f()\n"
|
||||||
|
"{\n"
|
||||||
|
" int x=1;\n"
|
||||||
|
" string s1, s2;\n"
|
||||||
|
" s1 = s1.substr();\n"
|
||||||
|
" s2 = s1.substr(x);\n"
|
||||||
|
" s1 = s2.substr(0, x);\n"
|
||||||
|
" s1 = s2.substr(0,std::string::npos);\n"
|
||||||
|
" s1 = s2.substr(x+5-n, 0);\n"
|
||||||
|
"};");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
check("int main()\n"
|
check("int main()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" std::string str = \"a1b1\";\n"
|
" std::string str = \"a1b1\";\n"
|
||||||
|
|
Loading…
Reference in New Issue