stlFindInsert: Take care of review comments
This commit is contained in:
parent
d2d53e5043
commit
a838cb65fb
|
@ -1595,9 +1595,13 @@ void CheckStl::checkFindInsertError(const Token *tok)
|
||||||
{
|
{
|
||||||
std::string replaceExpr;
|
std::string replaceExpr;
|
||||||
if (tok && Token::simpleMatch(tok->astParent(), "=") && tok == tok->astParent()->astOperand2() && Token::simpleMatch(tok->astParent()->astOperand1(), "[")) {
|
if (tok && Token::simpleMatch(tok->astParent(), "=") && tok == tok->astParent()->astOperand2() && Token::simpleMatch(tok->astParent()->astOperand1(), "[")) {
|
||||||
|
if (mSettings->standards.cpp < Standards::CPP11)
|
||||||
|
// We will recommend using emplace/try_emplace instead
|
||||||
|
return;
|
||||||
|
const std::string f = (mSettings->standards.cpp < Standards::CPP17) ? "emplace" : "try_emplace";
|
||||||
replaceExpr = " Instead of '" + tok->astParent()->expressionString() + "' consider using '" +
|
replaceExpr = " Instead of '" + tok->astParent()->expressionString() + "' consider using '" +
|
||||||
tok->astParent()->astOperand1()->astOperand1()->expressionString() +
|
tok->astParent()->astOperand1()->astOperand1()->expressionString() +
|
||||||
(mSettings->standards.cpp < Standards::CPP17 ? ".insert(" : ".emplace(") +
|
"." + f + "(" +
|
||||||
tok->astParent()->astOperand1()->astOperand2()->expressionString() +
|
tok->astParent()->astOperand1()->astOperand2()->expressionString() +
|
||||||
", " +
|
", " +
|
||||||
tok->expressionString() +
|
tok->expressionString() +
|
||||||
|
|
|
@ -5153,16 +5153,40 @@ private:
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
check("void foo() {\n"
|
{ // #10558
|
||||||
" std::map<int, int> x;\n"
|
check("void foo() {\n"
|
||||||
" int data = 0;\n"
|
" std::map<int, int> x;\n"
|
||||||
" for(int i=0; i<10; ++i) {\n"
|
" int data = 0;\n"
|
||||||
" data += 123;\n"
|
" for(int i=0; i<10; ++i) {\n"
|
||||||
" if(x.find(5) == x.end())\n"
|
" data += 123;\n"
|
||||||
" x[5] = data;\n"
|
" if(x.find(5) == x.end())\n"
|
||||||
" }\n"
|
" x[5] = data;\n"
|
||||||
"}");
|
" }\n"
|
||||||
ASSERT_EQUALS("[test.cpp:7]: (performance) Searching before insertion is not necessary. Instead of 'x[5]=data' consider using 'x.emplace(5, data);'.\n", errout.str());
|
"}", false, Standards::CPP03);
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
check("void foo() {\n"
|
||||||
|
" std::map<int, int> x;\n"
|
||||||
|
" int data = 0;\n"
|
||||||
|
" for(int i=0; i<10; ++i) {\n"
|
||||||
|
" data += 123;\n"
|
||||||
|
" if(x.find(5) == x.end())\n"
|
||||||
|
" x[5] = data;\n"
|
||||||
|
" }\n"
|
||||||
|
"}", false, Standards::CPP11);
|
||||||
|
ASSERT_EQUALS("[test.cpp:7]: (performance) Searching before insertion is not necessary. Instead of 'x[5]=data' consider using 'x.emplace(5, data);'.\n", errout.str());
|
||||||
|
|
||||||
|
check("void foo() {\n"
|
||||||
|
" std::map<int, int> x;\n"
|
||||||
|
" int data = 0;\n"
|
||||||
|
" for(int i=0; i<10; ++i) {\n"
|
||||||
|
" data += 123;\n"
|
||||||
|
" if(x.find(5) == x.end())\n"
|
||||||
|
" x[5] = data;\n"
|
||||||
|
" }\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:7]: (performance) Searching before insertion is not necessary. Instead of 'x[5]=data' consider using 'x.try_emplace(5, data);'.\n", errout.str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkKnownEmptyContainer() {
|
void checkKnownEmptyContainer() {
|
||||||
|
|
Loading…
Reference in New Issue