You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"The conversion from const char* as returned by c_str() to std::string creates an unnecessary string copy. Solve that by directly returning the string.", CWE704, Certainty::normal);
2104
2108
}
2105
2109
2106
-
voidCheckStl::string_c_strParam(const Token* tok, nonneg int number)
2110
+
voidCheckStl::string_c_strParam(const Token* tok, nonneg int number, const std::string& argtype)
2107
2111
{
2108
2112
std::ostringstream oss;
2109
-
oss << "Passing the result of c_str() to a function that takes std::string as argument no. " << number << " is slow and redundant.\n"
2110
-
"The conversion from const char* as returned by c_str() to std::string creates an unnecessary string copy. Solve that by directly passing the string.";
2113
+
oss << "Passing the result of c_str() to a function that takes " << argtype << " as argument no. " << number << " is slow and redundant.\n"
2114
+
"The conversion from const char* as returned by c_str() to " << argtype << "creates an unnecessary string copy or length calculation. Solve that by directly passing the string.";
Copy file name to clipboardExpand all lines: test/teststl.cpp
+7Lines changed: 7 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4195,6 +4195,13 @@ class TestStl : public TestFixture {
4195
4195
ASSERT_EQUALS("[test.cpp:6]: (performance) Assigning the result of c_str() to a std::string is slow and redundant.\n"
4196
4196
"[test.cpp:8]: (performance) Assigning the result of c_str() to a std::string is slow and redundant.\n",
4197
4197
errout.str());
4198
+
4199
+
check("void f(std::string_view);\n"// #11547
4200
+
"void g(const std::string & s) {\n"
4201
+
" f(s.c_str());\n"
4202
+
"}\n");
4203
+
ASSERT_EQUALS("[test.cpp:3]: (performance) Passing the result of c_str() to a function that takes std::string_view as argument no. 1 is slow and redundant.\n",
0 commit comments