Skip to content
Merged
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: 16 additions & 3 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,21 @@ static const ValueFlow::Value* getInnerLifetime(const Token* tok,
return nullptr;
}

static const Token* endOfExpression(const Token* tok)
{
if (!tok)
return nullptr;
const Token* parent = tok->astParent();
while (Token::simpleMatch(parent, "."))
parent = parent->astParent();
if (!parent)
return tok->next();
const Token* endToken = nextAfterAstRightmostLeaf(parent);
if (!endToken)
return parent->next();
return endToken;
}

void CheckStl::invalidContainer()
{
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
Expand Down Expand Up @@ -1120,9 +1135,7 @@ void CheckStl::invalidContainer()
}
if (Token::Match(assignExpr, "%assign%") && Token::Match(assignExpr->astOperand1(), "%var%"))
skipVarIds.insert(assignExpr->astOperand1()->varId());
const Token* endToken = nextAfterAstRightmostLeaf(tok->next()->astParent());
if (!endToken)
endToken = tok->next();
const Token* endToken = endOfExpression(tok);
const ValueFlow::Value* v = nullptr;
ErrorPath errorPath;
PathAnalysis::Info info =
Expand Down
12 changes: 12 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5465,6 +5465,18 @@ class TestStl : public TestFixture {
"}\n",
true);
ASSERT_EQUALS("", errout.str());

// #11147
check("void f(std::string& s) {\n"
" if (!s.empty()) {\n"
" std::string::iterator it = s.begin();\n"
" s = s.substr(it - s.begin());\n"
" }\n"
"}\n",
true);
ASSERT_EQUALS(
"[test.cpp:4]: (performance) Ineffective call of function 'substr' because a prefix of the string is assigned to itself. Use resize() or pop_back() instead.\n",
errout.str());
}

void invalidContainerLoop() {
Expand Down