Skip to content

Commit 6d33b3e

Browse files
committed
Fix #14600 FN stlcstrConstructor with offset (regression)
1 parent 7d80f64 commit 6d33b3e

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

lib/checkstl.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,20 @@ static bool isc_strConstructor(const Token* tok)
20032003
{
20042004
if (!tok->valueType() || !Token::Match(tok, "%var% (|{"))
20052005
return false;
2006-
return isc_strCall(tok->tokAt(1)->astOperand2(), tok->valueType()->container);
2006+
2007+
const Token* callTok = tok->tokAt(1)->astOperand2();
2008+
while (Token::simpleMatch(callTok, "+") && callTok->isBinaryOp()) {
2009+
if (callTok->astOperand1()->str() == "+") {
2010+
callTok = callTok->astOperand1();
2011+
continue;
2012+
}
2013+
if (callTok->astOperand1()->str() == "(")
2014+
callTok = callTok->astOperand1();
2015+
else if (tok->astOperand2()->str() == "(")
2016+
callTok = callTok->astOperand2();
2017+
break;
2018+
}
2019+
return isc_strCall(callTok, tok->valueType()->container);
20072020
}
20082021

20092022
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)