@@ -773,6 +773,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
773773 variables.clear ();
774774 break ;
775775 }
776+
777+ // templates
776778 if (tok->isName () && tok->str ().back () == ' >' ) {
777779 // TODO: This is a quick fix to handle when constants are used
778780 // as template parameters. Try to handle this better, perhaps
@@ -823,19 +825,30 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
823825 else if (Token::Match (tok->previous (), " [;{}]" )) {
824826 for (const Token* tok2 = tok->next (); tok2; tok2 = tok2->next ()) {
825827 if (tok2->varId ()) {
828+ // Is this a variable declaration?
826829 const Variable *var = tok2->variable ();
827- if (var && var->nameToken () == tok2) { // Declaration: Skip
828- tok = tok2->next ();
829- if (Token::Match (tok, " ( %name% )" )) // Simple initialization through copy ctor
830- tok = tok->next ();
831- else if (Token::Match (tok, " = %var% ;" )) { // Simple initialization
832- tok = tok->next ();
833- if (!var->isReference ())
834- variables.read (tok->varId (), tok);
835- } else if (var->typeEndToken ()->str () == " >" ) // Be careful with types like std::vector
836- tok = tok->previous ();
837- break ;
830+ if (!var || var->nameToken () != tok2)
831+ continue ;
832+
833+ // Mark template parameters used in declaration as use..
834+ if (tok2->strAt (-1 ) == " >" ) {
835+ for (const Token *tok3 = tok; tok3 != tok2; tok3 = tok3->next ()) {
836+ if (tok3->varId () > 0U )
837+ variables.use (tok3->varId (), tok3);
838+ }
838839 }
840+
841+ // Skip variable declaration..
842+ tok = tok2->next ();
843+ if (Token::Match (tok, " ( %name% )" )) // Simple initialization through copy ctor
844+ tok = tok->next ();
845+ else if (Token::Match (tok, " = %var% ;" )) { // Simple initialization
846+ tok = tok->next ();
847+ if (!var->isReference ())
848+ variables.read (tok->varId (), tok);
849+ } else if (var->typeEndToken ()->str () == " >" ) // Be careful with types like std::vector
850+ tok = tok->previous ();
851+ break ;
839852 } else if (Token::Match (tok2, " [;({=]" ))
840853 break ;
841854 }
0 commit comments