diff --git a/lib/astutils.cpp b/lib/astutils.cpp index da1a9f221..ac274bbe8 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -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) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 92e4eaf11..7e3f56a7d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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)