Improve check: uselessCallsConstructor (#4270)

This commit is contained in:
chrchr-github 2022-07-12 17:40:14 +02:00 committed by GitHub
parent 22be5f29aa
commit 381c38b2f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 0 deletions

View File

@ -2175,6 +2175,10 @@ void CheckStl::uselessCalls()
uselessCallsEmptyError(tok->next());
else if (Token::Match(tok, "[{};] std :: remove|remove_if|unique (") && tok->tokAt(5)->nextArgument())
uselessCallsRemoveError(tok->next(), tok->strAt(3));
else if (printPerformance && Token::Match(tok, "%var% = { %var% . begin ( ) ,") &&
tok->valueType() && tok->valueType()->type == ValueType::CONTAINER && tok->varId() == tok->tokAt(3)->varId()) {
uselessCallsConstructorError(tok);
}
}
}
}
@ -2223,6 +2227,12 @@ void CheckStl::uselessCallsSubstrError(const Token *tok, SubstrErrorType type)
reportError(tok, Severity::performance, "uselessCallsSubstr", msg, CWE398, Certainty::normal);
}
void CheckStl::uselessCallsConstructorError(const Token *tok)
{
const std::string msg = "Inefficient constructor call: container '" + tok->str() + "' is assigned a partial copy of itself. Use erase() or resize() instead.";
reportError(tok, Severity::performance, "uselessCallsConstructor", msg, CWE398, Certainty::normal);
}
void CheckStl::uselessCallsEmptyError(const Token *tok)
{
reportError(tok, Severity::warning, "uselessCallsEmpty", "Ineffective call of function 'empty()'. Did you intend to call 'clear()' instead?", CWE398, Certainty::normal);

View File

@ -224,6 +224,7 @@ private:
void uselessCallsSubstrError(const Token* tok, SubstrErrorType type);
void uselessCallsEmptyError(const Token* tok);
void uselessCallsRemoveError(const Token* tok, const std::string& function);
void uselessCallsConstructorError(const Token* tok);
void dereferenceInvalidIteratorError(const Token* deref, const std::string& iterName);
void dereferenceInvalidIteratorError(const Token* tok, const ValueFlow::Value *value, bool inconclusive);

View File

@ -4183,6 +4183,36 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (performance) Ineffective call of function 'substr' because a prefix of the string is assigned to itself. Use replace() instead.\n",
errout.str());
check("std::string f(std::string s, std::size_t end) {\n"
" s = { s.begin(), s.begin() + end };\n"
" return s;\n"
"}\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::list<int> f(std::list<int> l, std::size_t end) {\n"
" l = { l.begin(), l.begin() + end };\n"
" return l;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (performance) Inefficient constructor call: container 'l' is assigned a partial copy of itself. Use erase() or resize() instead.\n",
errout.str());
check("std::string f(std::string s, std::size_t end) {\n"
" s = std::string{ s.begin(), s.begin() + end };\n"
" return s;\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",
"",
errout.str());
check("std::string f(std::string s, std::size_t end) {\n"
" s = std::string(s.begin(), s.begin() + end);\n"
" return s;\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",
"",
errout.str());
}
void stabilityOfChecks() {