Refactoring; Check structured binding in isVariableChanged()

This commit is contained in:
Daniel Marjamäki 2021-05-08 20:46:46 +02:00
parent 895a96f9dd
commit 32b28d568f
2 changed files with 16 additions and 22 deletions

View File

@ -1890,6 +1890,22 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
if (parent && parent->tokType() == Token::eIncDecOp)
return true;
// structured binding, nonconst reference variable in lhs
if (Token::Match(tok2->astParent(), ":|=") && tok2 == tok2->astParent()->astOperand2() && Token::simpleMatch(tok2->astParent()->previous(), "]")) {
const Token *typeStart = tok2->astParent()->previous()->link()->previous();
if (Token::simpleMatch(typeStart, "&"))
typeStart = typeStart->previous();
if (typeStart && Token::Match(typeStart->previous(), "[;{}(] auto &| [")) {
for (const Token *vartok = typeStart->tokAt(2); vartok != tok2; vartok = vartok->next()) {
if (vartok->varId()) {
const Variable* refvar = vartok->variable();
if (!refvar || (!refvar->isConst() && refvar->isReference()))
return true;
}
}
}
}
if (Token::simpleMatch(tok2->astParent(), ":") && tok2->astParent()->astParent() && Token::simpleMatch(tok2->astParent()->astParent()->previous(), "for (")) {
const Token * varTok = tok2->astParent()->previous();
if (!varTok)

View File

@ -1419,28 +1419,6 @@ void CheckOther::checkConstVariable()
usedInAssignment = true;
break;
}
} else if (Token::Match(tok, "auto &| [")) {
// structured binding taking reference..
const Token *tok2 = tok->next();
if (tok2->str() == "&")
tok2 = tok2->next();
tok2 = tok2->link();
if (Token::Match(tok2, "] [:=] %varid% [);]", var->declarationId())) {
if (tok->next()->str() == "&") {
usedInAssignment = true;
break;
}
while (tok2 != tok) {
if (tok2->varId()) {
const Variable* refvar = tok2->variable();
if (!refvar || (!refvar->isConst() && refvar->isReference())) {
usedInAssignment = true;
break;
}
}
tok2 = tok2->previous();
}
}
}
}
if (usedInAssignment)