Fix 11147: FP invalidContainer with substr() (#4333)
* Fix 11147: FP invalidContainer with substr() * Format
This commit is contained in:
parent
dd927aab9b
commit
6cb3a79a64
|
@ -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();
|
||||
|
@ -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 =
|
||||
|
|
|
@ -5465,6 +5465,18 @@ private:
|
|||
"}\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() {
|
||||
|
|
Loading…
Reference in New Issue