From 57c055fcc41eda934df15653be5b6969dcca3a11 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sun, 3 Aug 2014 19:13:37 +0200 Subject: [PATCH] Fixed false negative #5985: default argument values should not affect variable usage checking. --- lib/checkunusedvar.cpp | 9 +++++++-- test/testunusedvar.cpp | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index a0464ad98..14d815de2 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -724,7 +724,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } // Check variable usage - for (const Token *tok = scope->classDef->next(); tok && tok != scope->classEnd; tok = tok->next()) { + const Token *tok; + if (scope->type == Scope::eFunction) + tok = scope->classStart->next(); + else + tok = scope->classDef->next(); + for (; tok && tok != scope->classEnd; tok = tok->next()) { if (tok->str() == "for" || tok->str() == "while" || tok->str() == "do") { for (std::list::const_iterator i = scope->nestedList.begin(); i != scope->nestedList.end(); ++i) { if ((*i)->classDef == tok) { // Find associated scope @@ -736,7 +741,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const if (!tok) break; } - if (tok->str() == "{") { + if (tok->str() == "{" && tok != scope->classStart) { for (std::list::const_iterator i = scope->nestedList.begin(); i != scope->nestedList.end(); ++i) { if ((*i)->classStart == tok) { // Find associated scope checkFunctionVariableUsage_iterateScopes(*i, variables, false); // Scan child scope diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 3f7238d7a..c07e869e4 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -862,6 +862,11 @@ private: " } while(code < 20);\n" "}"); ASSERT_EQUALS("", errout.str()); + + functionVariableUsage("void foo(int j = 0) {\n" // #5985 - default function parameters should not affect checking results + " int i = 0;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' is assigned a value that is never used.\n", errout.str()); } void localvar2() {