Fix uselessCallsConstructor TODOs (#4299)

* Fix uselessCallsConstructor TODOs

* Format
This commit is contained in:
chrchr-github 2022-07-21 22:15:16 +02:00 committed by GitHub
parent a64a5e1496
commit 79f091c59a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 9 deletions

View File

@ -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);
}
} }
} }
} }

View File

@ -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());
} }