Fixed #10558 (False positive; Searching before insertion in a loop is needed)

This commit is contained in:
Daniel Marjamäki 2021-10-24 19:13:42 +02:00
parent 70dcc9544e
commit d2d53e5043
2 changed files with 23 additions and 1 deletions

View File

@ -1593,8 +1593,19 @@ void CheckStl::checkFindInsert()
void CheckStl::checkFindInsertError(const Token *tok)
{
std::string replaceExpr;
if (tok && Token::simpleMatch(tok->astParent(), "=") && tok == tok->astParent()->astOperand2() && Token::simpleMatch(tok->astParent()->astOperand1(), "[")) {
replaceExpr = " Instead of '" + tok->astParent()->expressionString() + "' consider using '" +
tok->astParent()->astOperand1()->astOperand1()->expressionString() +
(mSettings->standards.cpp < Standards::CPP17 ? ".insert(" : ".emplace(") +
tok->astParent()->astOperand1()->astOperand2()->expressionString() +
", " +
tok->expressionString() +
");'.";
}
reportError(
tok, Severity::performance, "stlFindInsert", "Searching before insertion is not necessary.", CWE398, Certainty::normal);
tok, Severity::performance, "stlFindInsert", "Searching before insertion is not necessary." + replaceExpr, CWE398, Certainty::normal);
}
/**

View File

@ -5152,6 +5152,17 @@ private:
check(code, true, Standards::CPP17);
ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\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.emplace(5, data);'.\n", errout.str());
}
void checkKnownEmptyContainer() {