Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13327 FN mismatchingContainers for struct member and c_str() #7024

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,11 @@ static ValueFlow::Value getLifetimeIteratorValue(const Token* tok, MathLib::bigi
}
if (values.size() == 1)
return values.front();
it = std::find_if(values.cbegin(), values.cend(), [](const ValueFlow::Value& v) {
return v.tokvalue->str() == ".";
});
if (it != values.cend())
return *it;
return ValueFlow::Value{};
}

Expand Down
22 changes: 22 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TestStl : public TestFixture {
TEST_CASE(iterator28); // #10450
TEST_CASE(iterator29);
TEST_CASE(iterator30);
TEST_CASE(iterator31);
TEST_CASE(iteratorExpression);
TEST_CASE(iteratorSameExpression);
TEST_CASE(mismatchingContainerIterator);
Expand Down Expand Up @@ -1900,6 +1901,27 @@ class TestStl : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void iterator31()
{
check("struct S {\n" // #13327
" std::string a;\n"
"};\n"
"struct T {\n"
" S s;\n"
"};\n"
"bool f(const S& s) {\n"
" std::string b;\n"
" return s.a.c_str() == b.c_str();\n"
"}\n"
"bool g(const T& t) {\n"
" std::string b;\n"
" return t.s.a.c_str() == b.c_str();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Iterators of different containers 's.a' and 'b' are used together.\n"
"[test.cpp:13]: (error) Iterators of different containers 't.s.a' and 'b' are used together.\n",
errout_str());
}

void iteratorExpression() {
check("std::vector<int>& f();\n"
"std::vector<int>& g();\n"
Expand Down
Loading