Fixed #9218 (False positive: Searching before insertion is not necessary (stlFindInsert))

This commit is contained in:
Daniel Marjamäki 2021-01-14 22:31:43 +01:00
parent a5babf25a7
commit 55262f03ec
2 changed files with 18 additions and 0 deletions

View File

@ -1558,6 +1558,9 @@ void CheckStl::checkFindInsert()
std::tie(containerTok, keyTok) = isMapFind(condTok->astOperand1()); std::tie(containerTok, keyTok) = isMapFind(condTok->astOperand1());
if (!containerTok) if (!containerTok)
continue; continue;
// In < C++17 we only warn for small simple types
if (!(keyTok->valueType()->isIntegral() || keyTok->valueType()->pointer > 0) && mSettings->standards.cpp < Standards::CPP17)
continue;
const Token *thenTok = tok->next()->link()->next(); const Token *thenTok = tok->next()->link()->next();
const Token *valueTok = findInsertValue(thenTok, containerTok, keyTok, mSettings->library); const Token *valueTok = findInsertValue(thenTok, containerTok, keyTok, mSettings->library);

View File

@ -4726,6 +4726,21 @@ private:
"}\n", "}\n",
true); true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #9218 - not small type => do not warn if cpp standard is < c++17
{
const char code[] = "void f1(std::set<LargeType>& s, const LargeType& x) {\n"
" if (s.find(x) == s.end()) {\n"
" s.insert(x);\n"
" }\n"
"}\n";
check(code, true, Standards::CPP11);
ASSERT_EQUALS("", errout.str());
check(code, true, Standards::CPP14);
ASSERT_EQUALS("", errout.str());
check(code, true, Standards::CPP17);
ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout.str());
}
} }
void checkKnownEmptyContainer() { void checkKnownEmptyContainer() {