Skip to content

Fix #13105 (auto token with no type: member function of container owned by smart pointer) - #9044

Open
tropibyte wants to merge 2 commits into
cppcheck-opensource:mainfrom
tropibyte:fix-13105
Open

tropibyte wants to merge 2 commits into
cppcheck-opensource:mainfrom
tropibyte:fix-13105

Conversation

@tropibyte

Copy link
Copy Markdown
  • auto it = m->find(i) where m is a std::shared_ptr<std::map<int, int>> now gets an iterator type instead of autoNoType.
  • Iterators to containers owned by smart pointers are tracked as lifetimes, so invalidContainer, derefInvalidIterator and mismatchingContainers see them, including ranges passed to p->insert() / p->assign().
  • Two unique_ptrs can't own the same container, so mixing their iterators is an error (mismatchingContainers). Two shared_ptrs might, so it's a warning (mismatchingContainerExpression), matching the existing behavior for std::find.

Depends on #9042; the first commit here is that PR.

tropibyte and others added 2 commits November 17, 2026 11:09
…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 <[email protected]>
…ed by smart pointer)

'auto it = m->find(i)' where m is a std::shared_ptr<std::map<int, int>> left
'it' without a value type. Member functions called through '->' on a smart
pointer that owns a library container now get the container's return type.

Iterators to containers owned by smart pointers are now tracked as lifetimes,
so invalidContainer, derefInvalidIterator and mismatchingContainers detect
bugs with them, including iterator ranges passed to p->insert()/p->assign().
Shared pointers may own the same container, so mixing iterators from two
shared pointers is reported as mismatchingContainerExpression (warning).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant