diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 4d004af4e..4fdfaa305 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1959,8 +1959,8 @@ void CheckStl::string_c_str() string_c_strError(tok); } else if (printPerformance && tok->tokAt(1)->astOperand2() && Token::Match(tok->tokAt(1)->astOperand2()->tokAt(-3), "%var% . c_str|data ( ) ;")) { const Token* vartok = tok->tokAt(1)->astOperand2()->tokAt(-3); - if (tok->variable()->isStlStringType() && vartok->variable() && vartok->variable()->isStlStringType()) - string_c_strAssignment(tok); + if ((tok->variable()->isStlStringType() || tok->variable()->isStlStringViewType()) && vartok->variable() && vartok->variable()->isStlStringType()) + string_c_strAssignment(tok, tok->variable()->getTypeName()); } } else if (printPerformance && tok->function() && Token::Match(tok, "%name% ( !!)") && tok->str() != scope.className) { const auto range = c_strFuncParam.equal_range(tok->function()); @@ -1993,8 +1993,9 @@ void CheckStl::string_c_str() } } } else if (printPerformance && Token::Match(tok, "%var% (|{ %var% . c_str|data ( )") && - tok->variable() && tok->variable()->isStlStringType() && tok->tokAt(2)->variable() && tok->tokAt(2)->variable()->isStlStringType()) { - string_c_strConstructor(tok); + tok->variable() && (tok->variable()->isStlStringType() || tok->variable()->isStlStringViewType()) && + tok->tokAt(2)->variable() && tok->tokAt(2)->variable()->isStlStringType()) { + string_c_strConstructor(tok, tok->variable()->getTypeName()); } else if (printPerformance && tok->next() && tok->next()->variable() && tok->next()->variable()->isStlStringType() && tok->valueType() && tok->valueType()->type == ValueType::CONTAINER && ((Token::Match(tok->previous(), "%var% + %var% . c_str|data ( )") && tok->previous()->variable() && tok->previous()->variable()->isStlStringType()) || (Token::Match(tok->tokAt(-5), "%var% . c_str|data ( ) + %var%") && tok->tokAt(-5)->variable() && tok->tokAt(-5)->variable()->isStlStringType()))) { @@ -2115,17 +2116,17 @@ void CheckStl::string_c_strParam(const Token* tok, nonneg int number, const std: reportError(tok, Severity::performance, "stlcstrParam", oss.str(), CWE704, Certainty::normal); } -void CheckStl::string_c_strConstructor(const Token* tok) +void CheckStl::string_c_strConstructor(const Token* tok, const std::string& argtype) { - std::string msg = "Constructing a std::string from the result of c_str() is slow and redundant.\n" - "Constructing a std::string from const char* requires a call to strlen(). Solve that by directly passing the string."; + std::string msg = "Constructing a " + argtype + " from the result of c_str() is slow and redundant.\n" + "Constructing a " + argtype + " from const char* requires a call to strlen(). Solve that by directly passing the string."; reportError(tok, Severity::performance, "stlcstrConstructor", msg, CWE704, Certainty::normal); } -void CheckStl::string_c_strAssignment(const Token* tok) +void CheckStl::string_c_strAssignment(const Token* tok, const std::string& argtype) { - std::string msg = "Assigning the result of c_str() to a std::string is slow and redundant.\n" - "Assigning a const char* to a std::string requires a call to strlen(). Solve that by directly assigning the string."; + std::string msg = "Assigning the result of c_str() to a " + argtype + " is slow and redundant.\n" + "Assigning a const char* to a " + argtype + " requires a call to strlen(). Solve that by directly assigning the string."; reportError(tok, Severity::performance, "stlcstrAssignment", msg, CWE704, Certainty::normal); } diff --git a/lib/checkstl.h b/lib/checkstl.h index 55cc4f057..0fd1dac0a 100644 --- a/lib/checkstl.h +++ b/lib/checkstl.h @@ -193,8 +193,8 @@ private: void string_c_strError(const Token* tok); void string_c_strReturn(const Token* tok); void string_c_strParam(const Token* tok, nonneg int number, const std::string& argtype = "std::string"); - void string_c_strConstructor(const Token* tok); - void string_c_strAssignment(const Token* tok); + void string_c_strConstructor(const Token* tok, const std::string& argtype = "std::string"); + void string_c_strAssignment(const Token* tok, const std::string& argtype = "std::string"); void string_c_strConcat(const Token* tok); void string_c_strStream(const Token* tok); diff --git a/test/teststl.cpp b/test/teststl.cpp index be6771498..cc8ba3168 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -4202,6 +4202,18 @@ private: "}\n"); 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", errout.str()); + + check("std::string_view f(const std::string& s) {\n" + " std::string_view sv = s.c_str();\n" + " return sv;\n" + "}\n" + "std::string_view g(const std::string& s) {\n" + " std::string_view sv{ s.c_str() };\n" + " return sv;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (performance) Assigning the result of c_str() to a std::string_view is slow and redundant.\n" + "[test.cpp:6]: (performance) Constructing a std::string_view from the result of c_str() is slow and redundant.\n", + errout.str()); } void uselessCalls() {