@@ -1186,7 +1186,24 @@ void CheckUnusedVar::checkFunctionVariableUsage()
11861186 const Token* lambdaOrInlineStart{};
11871187 const bool hasLambdaOrInline = scope->hasInlineOrLambdaFunction (&lambdaOrInlineStart);
11881188
1189+ const Token *nextStructuredBindingTok = nullptr ;
1190+ std::vector<std::pair<const Token*, const Token*>> unusedStructuredBindingTokens;
1191+ size_t structuredBindingTokCount = 0 ;
1192+
11891193 for (const Token *tok = scope->bodyStart ; tok != scope->bodyEnd ; tok = tok->next ()) {
1194+ if (nextStructuredBindingTok) {
1195+ tok = nextStructuredBindingTok;
1196+ } else {
1197+ if (structuredBindingTokCount > 0 && structuredBindingTokCount == unusedStructuredBindingTokens.size ()) {
1198+ for (const auto &pair : unusedStructuredBindingTokens) {
1199+ unreadVariableError (pair.first , pair.second ->expressionString (), false );
1200+ }
1201+ }
1202+ structuredBindingTokCount = 0 ;
1203+ unusedStructuredBindingTokens.clear ();
1204+ }
1205+ nextStructuredBindingTok = nullptr ;
1206+
11901207 if (findLambdaEndToken (tok))
11911208 // todo: handle lambdas
11921209 break ;
@@ -1196,6 +1213,16 @@ void CheckUnusedVar::checkFunctionVariableUsage()
11961213 const Token *varDecl = nullptr ;
11971214 if (tok->variable () && tok->variable ()->nameToken () == tok) {
11981215 const Token * eq = tok->next ();
1216+ if (isStructuredBindingVariable (tok->variable ())) {
1217+ structuredBindingTokCount++;
1218+ while (!Token::simpleMatch (eq, " ]" )) {
1219+ eq = eq->next ();
1220+ if (eq->variable () && !nextStructuredBindingTok)
1221+ nextStructuredBindingTok = eq;
1222+ }
1223+ if (eq)
1224+ eq = eq->next ();
1225+ }
11991226 while (Token::simpleMatch (eq, " [" ))
12001227 eq = eq->link ()->next ();
12011228 if (Token::simpleMatch (eq, " ) (" ) && Token::simpleMatch (eq->linkAt (1 ), " ) =" ))
@@ -1333,8 +1360,12 @@ void CheckUnusedVar::checkFunctionVariableUsage()
13331360 continue ;
13341361
13351362 // warn
1336- if (!expr->variable () || !expr->variable ()->isMaybeUnused ())
1337- unreadVariableError (tok, expr->expressionString (), false );
1363+ if (!expr->variable () || !expr->variable ()->isMaybeUnused ()) {
1364+ if (structuredBindingTokCount > 0 )
1365+ unusedStructuredBindingTokens.emplace_back (tok, expr);
1366+ else
1367+ unreadVariableError (tok, expr->expressionString (), false );
1368+ }
13381369 }
13391370 }
13401371
0 commit comments