diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 844aa68a9..3b234196d 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -235,7 +235,7 @@ namespace { << "partition_point" << "pop_heap" << "prev_permutation" << "push_heap" << "random_shuffle" << "remove" << "remove_copy" << "remove_copy_if" << "remove_if" << "replace" << "replace_copy" << "replace_copy_if" << "replace_if" << "reverse" << "reverse_copy" << "search_n" << "shuffle" << "sort" << "sort_heap" << "stable_partition" << "stable_sort" << "swap_ranges" << "transform" << "unique" - << "unique_copy" << "upper_bound"; + << "unique_copy" << "upper_bound" << "string" << "wstring" << "u16string" << "u32string"; const std::set algorithm22 = make_container< std::set >() // func(begin1 << end1 << begin2 << end2 << "find_end" << "find_first_of" << "includes" << "lexicographical_compare" << "merge" << "partial_sort_copy" << "search" << "set_difference" << "set_intersection" << "set_symmetric_difference" << "set_union"; @@ -289,6 +289,14 @@ void CheckStl::mismatchingContainers() tok = arg1->linkAt(-1); } } + for (unsigned int varid = 0; varid < symbolDatabase->getVariableListSize(); varid++) { + const Variable* var = symbolDatabase->getVariableFromVarId(varid); + if (var && var->isStlStringType() && Token::Match(var->nameToken(), "%var% (") && Token::Match(var->nameToken()->tokAt(2), pattern2.c_str())) { + if (var->nameToken()->strAt(2) != var->nameToken()->strAt(8)) { + mismatchingContainersError(var->nameToken()); + } + } + } } diff --git a/test/teststl.cpp b/test/teststl.cpp index 46755a765..2a31e0c75 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -296,6 +296,20 @@ private: " std::vector::iterator it = std::find_first_of(ints1.begin(), ints1.end(), ints2.begin(), ints2.end());\n" "}"); ASSERT_EQUALS("", errout.str()); + + // #6839 + check("void f(const std::wstring& a, const std::wstring& b) {\n" + " const std::string tp1 = std::string(a.begin(), b.end());\n" + " const std::wstring tp2 = std::string(b.begin(), a.end());\n" + " const std::u16string tp3(a.begin(), b.end());\n" + " const std::u32string tp4(b.begin(), a.end());\n" + " const std::string fp1 = std::string(a.begin(), a.end());\n" + " const std::string tp2(a.begin(), a.end());\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Iterators of different containers are used together.\n" + "[test.cpp:3]: (error) Iterators of different containers are used together.\n" + "[test.cpp:4]: (error) Iterators of different containers are used together.\n" + "[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str()); } void iterator9() {