diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index aa08725430a..c72b50dfd5a 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1636,6 +1636,10 @@ static std::pair isMapFind(const Token *tok) return {}; if (!tok->astOperand2()) return {}; + if (!Token::simpleMatch(tok->astParent(), "==")) + return {}; + if (tok->astOperand1()->strAt(1) == "count" && (!tok->astParent()->astOperand2()->hasKnownIntValue() || tok->astParent()->astOperand2()->getKnownIntValue() != 0)) + return {}; return {contTok, tok->astOperand2()}; } diff --git a/test/teststl.cpp b/test/teststl.cpp index bb20d2d8182..737f1c8e57e 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -7424,6 +7424,17 @@ class TestStl : public TestFixture { dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); + check("void f(std::map& m) {\n" // #15116 + " if (m.count(1) != 0)\n" + " m[1] = true;\n" + "}\n" + "void g(std::map& m) {\n" + " if (m.count(1) == 1)\n" + " m[1] = true;\n" + "}\n", + dinit(CheckOptions, $.inconclusive = true)); + ASSERT_EQUALS("", errout_str()); // TODO: warn about duplicate search + // #9218 - not small type => do not warn if cpp standard is < c++17 { Settings s = settings;