diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index d23bb5a0e..19c8f4c38 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -591,6 +591,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const if (scope->type != Scope::eClass && scope->type != Scope::eUnion && scope->type != Scope::eStruct) { // Find declarations for (std::list::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) { + if (i->isThrow()) + continue; Variables::VariableType type = Variables::none; if (i->isArray() && (i->nameToken()->previous()->str() == "*" || i->nameToken()->strAt(-2) == "*")) type = Variables::pointerArray; @@ -639,7 +641,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const // Check variable usage for (const Token *tok = scope->classDef->next(); tok && tok != scope->classEnd; tok = tok->next()) { - if (tok->str() == "for" || tok->str() == "catch") { + if (tok->str() == "for") { for (std::list::const_iterator i = scope->nestedList.begin(); i != scope->nestedList.end(); ++i) { if ((*i)->classDef == tok) { // Find associated scope checkFunctionVariableUsage_iterateScopes(*i, variables); // Scan child scope diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 2e9f702ee..6e486a496 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -110,6 +110,8 @@ private: TEST_CASE(localvarconst1); TEST_CASE(localvarconst2); + TEST_CASE(localvarthrow); // ticket #3687 + // Don't give false positives for variables in structs/unions TEST_CASE(localvarStruct1); TEST_CASE(localvarStruct2); @@ -2998,6 +3000,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvarthrow() { // ticket #3687 + functionVariableUsage("void foo() {\n" + " try {}" + " catch(Foo& bar) {}\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + // ticket #3104 - false positive when variable is read with "if (NOT var)" void localvarIfNOT() { functionVariableUsage("void f() {\n"