From e7aa1ec396564ef41e2df77c7bd5ac1a82f763a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 29 Dec 2012 12:45:37 +0100 Subject: [PATCH] Fixed #4411 (Variable is assigned a value that is never used.) --- lib/checkunusedvar.cpp | 5 ++++- test/testunusedvar.cpp | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index f4b1da635..d353d7492 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -776,8 +776,11 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const tok = tok2->next(); if (Token::Match(tok, "( %var% )")) // Simple initialization through copy ctor tok = tok->next(); - else if (Token::Match(tok, "= %var% ;")) // Simple initialization + else if (Token::Match(tok, "= %var% ;")) { // Simple initialization tok = tok->next(); + if (!var->isReference()) + variables.read(tok->varId(), tok); + } else if (var->typeEndToken()->str() == ">") // Be careful with types like std::vector tok = tok->previous(); break; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 019d805a1..6e3b023e0 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -1148,6 +1148,14 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'i' is assigned a value that is never used.\n", errout.str()); + functionVariableUsage("double foo()\n" + "{\n" + " double i = 0.0;\n" + " const double j = i;\n" + " return j;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + functionVariableUsage("void foo()\n" "{\n" " A * i;\n"