Repository navigation
Conversation
…ntainer member functions Iterator ranges passed to insert(), assign(), append() and replace() of a container were not checked, so 'ints2.insert(it1, it2)' with iterators from two different containers went unreported (TODO in TestStl::iterator6). To support this, library container detection now recognizes nested iterator types of non-template containers and of std::span, e.g. std::string::iterator and std::span<int>::iterator. The stale TODO in CheckStl::iterators() now points at mismatchingContainers(), which performs the check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Test results for commit 33a5aec (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9042/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
|
There should probably be a trac ticket for this PR. Please create it, or ask us to create it. Then the PR title should be updated. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The new check works on every case I tried, and I found no false positive or crash in mismatchingContainers.
One new problem: variables of type std::string::iterator now get an iterator value type. Because of that, std::string parameters now get the same constParameterReference false positive that std::vector already gets. std::string::iterator it = s.begin(); makes cppcheck say that s can be const std::string&, but with a const string begin() returns a const_iterator, so that code would not compile. The base version does not report this for strings.
Nothing else that is new: the unit tests pass, and the cfg tests and the files I checked from cppcheck's own sources give the same results as base.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | std::string::iterator detection adds constParameterReference false positives | AI review | cfg/std.cfg:9109 |
🆕 New |
@tropibyte, please fix, resolve or reply to the open and new comments: #1.
Testing
Built cppcheck and testrunner in pr/, and cppcheck in base/.
./testrunner TestStl TestLibrary TestSymbolDatabase TestValueFlow TestAutoVariables: 0 failures.test/cfg/std.cpp,qt.cpp,wxwidgets.cppandmicrosoft_gsl.cppchecked with both binaries: same output.lib/mathlib.cpp,lib/utils.cppandlib/library.cppchecked with both binaries: same output.
mismatchingContainers cases (scratch):
- Found as expected:
l.insert(l.end(), a.begin(), b.end())andl.assign(a.begin(), b.end())onstd::lists.replace(s.begin(), s.end(), a.begin(), b.end())with vectors as the source- iterator variables, e.g.
first = a.begin(); last = v.end(); v.insert(v.begin(), first, last)
- No warning, as expected:
insert(hint, value)onset<vector<int>::iterator>, with the hint from a parameter,s.find(..),std::next(s.begin()),std::prev(s.end()),ps->end(),m[1].end(),vs[i].end(),b.s.end(),this->s.end(), a reference alias, orh = s.insert(h, ..)in a loopinsert(pos, n, it)andassign(n, it)onvector<iterator>- conditional ranges like
c ? a.begin() : s.begin()
New false positive (reproduced):
#include <string>
int f(std::string& s) {
std::string::iterator it = s.begin();
return *it;
}cppcheck -q --enable=style c1.cpp
- base: no output
- PR:
c1.cpp:3:20: style: Parameter 's' can be declared as reference to const [constParameterReference]
The same pattern with std::vector<int>& already gives this warning in base. The PR makes strings behave the same way, and the warning is wrong for both, because a const_iterator can not be converted to iterator. A wider comparison of std::string code showed the same new warning in a loop with for (std::string::iterator it = s.begin(); ...).
Automated review of 33a5aec by Claude. The pull request was built and tested locally.
| <type templateParameter="0"/> | ||
| </container> | ||
| <container id="stdString" startPattern="std :: string|wstring|u16string|u32string" endPattern="" inherits="stdAllString"/> | ||
| <container id="stdString" startPattern="std :: string|wstring|u16string|u32string" endPattern="" itEndPattern=":: iterator|const_iterator|reverse_iterator|const_reverse_iterator" inherits="stdAllString"/> |
There was a problem hiding this comment.
AI review
With this itEndPattern, std::string::iterator variables become iterators, and that brings a new constParameterReference false positive for std::string& parameters:
int f(std::string& s) {
std::string::iterator it = s.begin();
return *it;
}PR: c1.cpp:3:20: style: Parameter 's' can be declared as reference to const [constParameterReference]. Base: no warning.
If s is const, s.begin() returns a const_iterator, which can not be converted to std::string::iterator, so following the suggestion breaks the build. std::vector<int>::iterator already has the same false positive in base. Since this PR spreads it to every string, the const check should treat an initialization of a non-const iterator variable from begin()/end() etc. as a non-const use. At least add a test for this case.
insert,assign,appendandreplaceof a container, soints2.insert(it1, it2)with iterators from different containers is now reported.insert(hint, value)on associative containers, where the first iterator belongs to the container itself, is excluded.std::span(std::string::iterator,std::span<int>::iterator), so such variables get an iterator value type.std::span<int>::size_typeis no longer treated as a container.TODOinCheckStl::iterators()with a pointer tomismatchingContainers(), which now does that check.TODO_ASSERTinTestStl::iterator6and five inTestLibrary, and adds positive and negative cases.There is no Trac ticket for this yet. Could one of you file one? Once it exists I'll add
Fix #NNNNNto the commit title.