Skip to content

Commit 55262f0

Browse files
committed
Fixed #9218 (False positive: Searching before insertion is not necessary (stlFindInsert))
1 parent a5babf2 commit 55262f0

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

lib/checkstl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,9 @@ void CheckStl::checkFindInsert()
15581558
std::tie(containerTok, keyTok) = isMapFind(condTok->astOperand1());
15591559
if (!containerTok)
15601560
continue;
1561+
// In < C++17 we only warn for small simple types
1562+
if (!(keyTok->valueType()->isIntegral() || keyTok->valueType()->pointer > 0) && mSettings->standards.cpp < Standards::CPP17)
1563+
continue;
15611564

15621565
const Token *thenTok = tok->next()->link()->next();
15631566
const Token *valueTok = findInsertValue(thenTok, containerTok, keyTok, mSettings->library);

test/teststl.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4726,6 +4726,21 @@ class TestStl : public TestFixture {
47264726
"}\n",
47274727
true);
47284728
ASSERT_EQUALS("", errout.str());
4729+
4730+
// #9218 - not small type => do not warn if cpp standard is < c++17
4731+
{
4732+
const char code[] = "void f1(std::set<LargeType>& s, const LargeType& x) {\n"
4733+
" if (s.find(x) == s.end()) {\n"
4734+
" s.insert(x);\n"
4735+
" }\n"
4736+
"}\n";
4737+
check(code, true, Standards::CPP11);
4738+
ASSERT_EQUALS("", errout.str());
4739+
check(code, true, Standards::CPP14);
4740+
ASSERT_EQUALS("", errout.str());
4741+
check(code, true, Standards::CPP17);
4742+
ASSERT_EQUALS("[test.cpp:3]: (performance) Searching before insertion is not necessary.\n", errout.str());
4743+
}
47294744
}
47304745

47314746
void checkKnownEmptyContainer() {

0 commit comments

Comments
 (0)