Skip to content

Commit d412fe2

Browse files
Fix #15040 FN stlcstrConcat with compound assignment (#8857)
1 parent f90a76f commit d412fe2

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎lib/checkstl.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1986,9 +1986,11 @@ static bool isc_strCall(const Token* tok, const Library::Container* container)
19861986

19871987
static bool isc_strConcat(const Token* tok)
19881988
{
1989-
if (!tok->isBinaryOp() || !Token::simpleMatch(tok, "+"))
1989+
if (!tok->isBinaryOp() || !Token::Match(tok, "+|+="))
19901990
return false;
19911991
for (const Token* op : { tok->astOperand1(), tok->astOperand2() }) { // NOLINT(readability-use-anyofallof)
1992+
if (tok->isAssignmentOp() && astIsLHS(op))
1993+
continue;
19921994
const Token* sibling = op->astSibling();
19931995
if (!sibling->valueType())
19941996
continue;

‎test/teststl.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5161,6 +5161,12 @@ class TestStl : public TestFixture {
51615161
"}\n");
51625162
ASSERT_EQUALS("[test.cpp:2:17]: (performance) Constructing a std::string from the result of c_str() is slow and redundant. [stlcstrConstructor]\n",
51635163
errout_str());
5164+
5165+
check("void f(std::string& a, const std::string& b) {\n"
5166+
" a += b.c_str();\n"
5167+
"}\n");
5168+
ASSERT_EQUALS("[test.cpp:2:7]: (performance) Concatenating the result of c_str() and a std::string is slow and redundant. [stlcstrConcat]\n",
5169+
errout_str());
51645170
}
51655171

51665172
void uselessCalls() {

0 commit comments

Comments
 (0)