From 3bd5a4d10efbca476b42e4479275ea3f628f2cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 1 Jan 2016 16:04:13 +0100 Subject: [PATCH] CheckUnusedVar: Fix FP when there is class initialization --- lib/checkunusedvar.cpp | 6 +++++- test/testunusedvar.cpp | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 84c75fd14..54816f861 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1060,7 +1060,11 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } else if (tok->varId() && tok->next() && (tok->next()->str() == ")" || tok->next()->isExtendedOp())) { - variables.readAll(tok->varId(), tok); + if (Token::Match(tok->tokAt(-2), "%name% ( %var% [,)]") && + !(tok->tokAt(-2)->variable() && tok->tokAt(-2)->variable()->isReference())) + variables.use(tok->varId(), tok); + else + variables.readAll(tok->varId(), tok); } else if (Token::Match(tok, "%var% ;") && Token::Match(tok->previous(), "[;{}:]")) { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 8b0377f0e..e99289577 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -157,6 +157,7 @@ private: TEST_CASE(localvarRangeBasedFor); // #7075 TEST_CASE(localvarAssignInWhile); + TEST_CASE(localvarCppInitialization); TEST_CASE(localvarCpp11Initialization); TEST_CASE(chainedAssignment); // #5466 @@ -3832,6 +3833,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvarCppInitialization() { + functionVariableUsage("void foo() {\n" + " int buf[6];\n" + " Data data(buf);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void localvarCpp11Initialization() { // #6160 functionVariableUsage("void foo() {\n"