Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,23 @@ static bool isc_strConcat(const Token* tok)
return false;
}

static bool isc_strInPlusChain(const Token* tok, const Library::Container* container)
{
if (!tok || !tok->valueType() || !tok->valueType()->pointer)
return false;
bool result = false;
visitAstNodes(tok, [&](const Token* tok2) {
if (Token::simpleMatch(tok2, "+"))
return ChildrenToVisit::op1_and_op2;
if (isc_strCall(tok2, container)) {
result = true;
return ChildrenToVisit::done;
}
return ChildrenToVisit::none;
});
return result;
}

static bool isc_strAssignment(const Token* tok)
{
if (!Token::simpleMatch(tok, "="))
Expand All @@ -2003,7 +2020,7 @@ static bool isc_strConstructor(const Token* tok)
{
if (!tok->valueType() || !Token::Match(tok, "%var% (|{"))
return false;
return isc_strCall(tok->tokAt(1)->astOperand2(), tok->valueType()->container);
return isc_strInPlusChain(tok->tokAt(1)->astOperand2(), tok->valueType()->container);
}

namespace {
Expand Down
7 changes: 7 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4719,6 +4719,13 @@ class TestStl : public TestFixture {
" auto a = + s.c_str();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("std::string f(const std::string& a) {\n" // #14600
" std::string b(a.c_str() + 1 + 2);\n"
" return b;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:17]: (performance) Constructing a std::string from the result of c_str() is slow and redundant. [stlcstrConstructor]\n",
errout_str());
}

void uselessCalls() {
Expand Down
Loading