Skip to content

Commit 57149b4

Browse files
Fix #14600 FN stlcstrConstructor with offset (regression) (#8338)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent cf93424 commit 57149b4

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lib/checkstl.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,23 @@ static bool isc_strConcat(const Token* tok)
19891989
return false;
19901990
}
19911991

1992+
static bool isc_strInPlusChain(const Token* tok, const Library::Container* container)
1993+
{
1994+
if (!tok || !tok->valueType() || !tok->valueType()->pointer)
1995+
return false;
1996+
bool result = false;
1997+
visitAstNodes(tok, [&](const Token* tok2) {
1998+
if (Token::simpleMatch(tok2, "+"))
1999+
return ChildrenToVisit::op1_and_op2;
2000+
if (isc_strCall(tok2, container)) {
2001+
result = true;
2002+
return ChildrenToVisit::done;
2003+
}
2004+
return ChildrenToVisit::none;
2005+
});
2006+
return result;
2007+
}
2008+
19922009
static bool isc_strAssignment(const Token* tok)
19932010
{
19942011
if (!Token::simpleMatch(tok, "="))
@@ -2003,7 +2020,7 @@ static bool isc_strConstructor(const Token* tok)
20032020
{
20042021
if (!tok->valueType() || !Token::Match(tok, "%var% (|{"))
20052022
return false;
2006-
return isc_strCall(tok->tokAt(1)->astOperand2(), tok->valueType()->container);
2023+
return isc_strInPlusChain(tok->tokAt(1)->astOperand2(), tok->valueType()->container);
20072024
}
20082025

20092026
namespace {

test/teststl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4719,6 +4719,13 @@ class TestStl : public TestFixture {
47194719
" auto a = + s.c_str();\n"
47204720
"}\n");
47214721
ASSERT_EQUALS("", errout_str());
4722+
4723+
check("std::string f(const std::string& a) {\n" // #14600
4724+
" std::string b(a.c_str() + 1 + 2);\n"
4725+
" return b;\n"
4726+
"}\n");
4727+
ASSERT_EQUALS("[test.cpp:2:17]: (performance) Constructing a std::string from the result of c_str() is slow and redundant. [stlcstrConstructor]\n",
4728+
errout_str());
47224729
}
47234730

47244731
void uselessCalls() {

0 commit comments

Comments
 (0)