Skip to content
4 changes: 2 additions & 2 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1937,10 +1937,10 @@ void CheckStl::string_c_str()
}

auto isString = [](const Token* str) -> bool {
while (Token::Match(str, "::|."))
str = str->astOperand2();
if (Token::Match(str, "(|[") && !(str->valueType() && str->valueType()->type == ValueType::ITERATOR))
str = str->previous();
else if (Token::simpleMatch(str, "."))
str = str->astOperand2();
return str && ((str->variable() && str->variable()->isStlStringType()) || // variable
(str->function() && isStlStringType(str->function()->retDef)) || // function returning string
(str->valueType() && str->valueType()->type == ValueType::ITERATOR && isStlStringType(str->valueType()->containerTypeToken))); // iterator pointing to string
Expand Down
9 changes: 6 additions & 3 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3900,19 +3900,22 @@ class TestStl : public TestFixture {
" std::string a[1];\n"
" struct U { std::string s; } u;"
"};\n"
"namespace N { namespace O { std::string s; } }\n"
"void g(const std::vector<std::string>& v, T& t) {\n"
" for (std::vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it)\n"
" f(it->c_str());\n"
" f(v.begin()->c_str());\n"
" f(t.g().c_str());\n"
" f(t.a[0].c_str());\n"
" f(t.u.s.c_str());\n"
" f(N::O::s.c_str());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
"[test.cpp:9]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
ASSERT_EQUALS("[test.cpp:9]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
"[test.cpp:10]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
"[test.cpp:11]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
"[test.cpp:12]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n",
"[test.cpp:12]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
"[test.cpp:13]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n"
"[test.cpp:14]: (performance) Passing the result of c_str() to a function that takes std::string as argument no. 1 is slow and redundant.\n",
errout.str());

check("void svgFile(const std::string &content, const std::string &fileName, const double end = 1000., const double start = 0.);\n"
Expand Down