From 936043d47dcb1bb24ad1e1feddbc18c682a1589c Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 30 Sep 2014 12:39:27 +0200 Subject: [PATCH] Fixed false positive 'unusedVar' with C++11 initialization (#6160) --- lib/checkunusedvar.cpp | 4 ++-- test/testunusedvar.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index eb47d7f1c..6f02fcf20 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1025,8 +1025,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const variables.read(tok->varId(), tok); } - else if (Token::Match(tok, "[{,] %var% [,}]")) { - variables.read(tok->next()->varId(), tok); + else if (Token::Match(tok->previous(), "[{,] %var% [,}]")) { + variables.read(tok->varId(), tok); } else if (tok->varId() && Token::Match(tok, "%var% .")) { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index aa919a5aa..59e072d19 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -156,6 +156,8 @@ private: TEST_CASE(localvarNULL); // #4203 - Setting NULL value is not redundant - it is safe TEST_CASE(localvarUnusedGoto); // #4447, #4558 goto + TEST_CASE(localvarCpp11Initialization); + TEST_CASE(chainedAssignment); // #5466 TEST_CASE(crash1); @@ -3775,6 +3777,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvarCpp11Initialization() { + // #6160 + functionVariableUsage("void foo() {\n" + " int myNewValue{ 3u };\n" + " myManager.theDummyTable.addRow(UnsignedIndexValue{ myNewValue }, DummyRowData{ false });\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void chainedAssignment() { // #5466 functionVariableUsage("void NotUsed(double* pdD, int n) {\n"