Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,10 @@ static std::pair<const Token *, const Token *> isMapFind(const Token *tok)
return {};
if (!tok->astOperand2())
return {};
if (!Token::simpleMatch(tok->astParent(), "=="))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

Requiring == is too narrow for count(), because the operator alone does not say whether the condition means "key absent". if (s.count(x) != 1) s.insert(x); on a std::set, and if (m.count(x) < 1) m[x] = 1;, both mean "absent". Base reported stlFindInsert for both, and with this PR neither warns: lost true positives.

The fix should check the meaning of the comparison: absent means == 0, != 1 (for a set), < 1 or <= 0. It should not filter on == only. A test for one of these cases would help too.

return {};
if (tok->astOperand1()->strAt(1) == "count" && (!tok->astParent()->astOperand2()->hasKnownIntValue() || tok->astParent()->astOperand2()->getKnownIntValue() != 0))
return {};
return {contTok, tok->astOperand2()};
}

Expand Down
11 changes: 11 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7424,6 +7424,17 @@ class TestStl : public TestFixture {
dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("", errout_str());

check("void f(std::map<int, bool>& m) {\n" // #15116
" if (m.count(1) != 0)\n"
" m[1] = true;\n"
"}\n"
"void g(std::map<int, bool>& 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing the warning here? We should just update the warning message.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to add the logic necessary to generate a fitting error message in this case. The condition handling also needs tweaking, see https://trac.cppcheck.net/ticket/15146

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I update the logic for the error message in #8975.


// #9218 - not small type => do not warn if cpp standard is < c++17
{
Settings s = settings;
Expand Down
Loading