Fix uselessCallsConstructor TODOs (#4299)
* Fix uselessCallsConstructor TODOs * Format
This commit is contained in:
parent
a64a5e1496
commit
79f091c59a
|
@ -2175,9 +2175,19 @@ void CheckStl::uselessCalls()
|
||||||
uselessCallsEmptyError(tok->next());
|
uselessCallsEmptyError(tok->next());
|
||||||
else if (Token::Match(tok, "[{};] std :: remove|remove_if|unique (") && tok->tokAt(5)->nextArgument())
|
else if (Token::Match(tok, "[{};] std :: remove|remove_if|unique (") && tok->tokAt(5)->nextArgument())
|
||||||
uselessCallsRemoveError(tok->next(), tok->strAt(3));
|
uselessCallsRemoveError(tok->next(), tok->strAt(3));
|
||||||
else if (printPerformance && Token::Match(tok, "%var% = { %var% . begin ( ) ,") &&
|
else if (printPerformance && tok->valueType() && tok->valueType()->type == ValueType::CONTAINER) {
|
||||||
tok->valueType() && tok->valueType()->type == ValueType::CONTAINER && tok->varId() == tok->tokAt(3)->varId()) {
|
if (Token::Match(tok, "%var% = { %var% . begin ( ) ,") && tok->varId() == tok->tokAt(3)->varId())
|
||||||
uselessCallsConstructorError(tok);
|
uselessCallsConstructorError(tok);
|
||||||
|
else if (const Variable* var = tok->variable()) {
|
||||||
|
std::string pattern = "%var% = ";
|
||||||
|
for (const Token* t = var->typeStartToken(); t != var->typeEndToken()->next(); t = t->next()) {
|
||||||
|
pattern += t->str();
|
||||||
|
pattern += ' ';
|
||||||
|
}
|
||||||
|
pattern += "{|( %varid% . begin ( ) ,";
|
||||||
|
if (Token::Match(tok, pattern.c_str(), tok->varId()))
|
||||||
|
uselessCallsConstructorError(tok);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4202,16 +4202,21 @@ private:
|
||||||
" s = std::string{ s.begin(), s.begin() + end };\n"
|
" s = std::string{ s.begin(), s.begin() + end };\n"
|
||||||
" return s;\n"
|
" return s;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
TODO_ASSERT_EQUALS("[test.cpp:2]: (performance) Inefficient constructor call: container 's' is assigned a partial copy of itself. Use erase() or resize() instead.\n",
|
ASSERT_EQUALS("[test.cpp:2]: (performance) Inefficient constructor call: container 's' is assigned a partial copy of itself. Use erase() or resize() instead.\n",
|
||||||
"",
|
|
||||||
errout.str());
|
errout.str());
|
||||||
|
|
||||||
check("std::string f(std::string s, std::size_t end) {\n"
|
check("std::string f(std::string s, std::size_t end) {\n"
|
||||||
" s = std::string(s.begin(), s.begin() + end);\n"
|
" s = std::string(s.begin(), s.begin() + end);\n"
|
||||||
" return s;\n"
|
" return s;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
TODO_ASSERT_EQUALS("[test.cpp:2]: (performance) Inefficient constructor call: container 's' is assigned a partial copy of itself. Use erase() or resize() instead.\n",
|
ASSERT_EQUALS("[test.cpp:2]: (performance) Inefficient constructor call: container 's' is assigned a partial copy of itself. Use erase() or resize() instead.\n",
|
||||||
"",
|
errout.str());
|
||||||
|
|
||||||
|
check("std::vector<int> f(std::vector<int> v, std::size_t end) {\n"
|
||||||
|
" v = std::vector<int>(v.begin(), v.begin() + end);\n"
|
||||||
|
" return v;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:2]: (performance) Inefficient constructor call: container 'v' is assigned a partial copy of itself. Use erase() or resize() instead.\n",
|
||||||
errout.str());
|
errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue