Fix c_strParam regression (#4393)
* Fix #7515 New check: Not needed c_str() operation * Comment * Detect more instances of c_str() misuse * Fix bad merge * Check for data() also * Fix * Format * Format * Fix c_strParam regression
This commit is contained in:
parent
5d267000e2
commit
4be7f689d7
|
@ -1939,6 +1939,8 @@ void CheckStl::string_c_str()
|
||||||
auto isString = [](const Token* str) -> bool {
|
auto isString = [](const Token* str) -> bool {
|
||||||
if (Token::Match(str, "(|[") && !(str->valueType() && str->valueType()->type == ValueType::ITERATOR))
|
if (Token::Match(str, "(|[") && !(str->valueType() && str->valueType()->type == ValueType::ITERATOR))
|
||||||
str = str->previous();
|
str = str->previous();
|
||||||
|
else if (Token::simpleMatch(str, "."))
|
||||||
|
str = str->astOperand2();
|
||||||
return str && ((str->variable() && str->variable()->isStlStringType()) || // variable
|
return str && ((str->variable() && str->variable()->isStlStringType()) || // variable
|
||||||
(str->function() && isStlStringType(str->function()->retDef)) || // function returning string
|
(str->function() && isStlStringType(str->function()->retDef)) || // function returning string
|
||||||
(str->valueType() && str->valueType()->type == ValueType::ITERATOR && isStlStringType(str->valueType()->containerTypeToken))); // iterator pointing to string
|
(str->valueType() && str->valueType()->type == ValueType::ITERATOR && isStlStringType(str->valueType()->containerTypeToken))); // iterator pointing to string
|
||||||
|
|
|
@ -3898,6 +3898,7 @@ private:
|
||||||
"struct T {\n"
|
"struct T {\n"
|
||||||
" std::string g();\n"
|
" std::string g();\n"
|
||||||
" std::string a[1];\n"
|
" std::string a[1];\n"
|
||||||
|
" struct U { std::string s; } u;"
|
||||||
"};\n"
|
"};\n"
|
||||||
"void g(const std::vector<std::string>& v, T& t) {\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"
|
" for (std::vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it)\n"
|
||||||
|
@ -3905,11 +3906,13 @@ private:
|
||||||
" f(v.begin()->c_str());\n"
|
" f(v.begin()->c_str());\n"
|
||||||
" f(t.g().c_str());\n"
|
" f(t.g().c_str());\n"
|
||||||
" f(t.a[0].c_str());\n"
|
" f(t.a[0].c_str());\n"
|
||||||
|
" f(t.u.s.c_str());\n"
|
||||||
"}\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"
|
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"
|
"[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: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: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",
|
||||||
errout.str());
|
errout.str());
|
||||||
|
|
||||||
check("void svgFile(const std::string &content, const std::string &fileName, const double end = 1000., const double start = 0.);\n"
|
check("void svgFile(const std::string &content, const std::string &fileName, const double end = 1000., const double start = 0.);\n"
|
||||||
|
|
Loading…
Reference in New Issue