From 95dc05b21d3e3fe8bf56dfed69e36e84593f7835 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 13 Jan 2022 08:05:05 +0100 Subject: [PATCH] Fix #7732 false negative: (style) Unused variable: std::pair (#3695) --- cfg/qt.cfg | 6 ++++++ cfg/std.cfg | 6 ++++++ lib/checkunusedvar.cpp | 6 ++++-- test/testunusedvar.cpp | 13 ++++++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cfg/qt.cfg b/cfg/qt.cfg index 27c3f5882..2362b7f3b 100644 --- a/cfg/qt.cfg +++ b/cfg/qt.cfg @@ -5104,6 +5104,12 @@ + + + QApplication + QMutexLocker + + diff --git a/cfg/std.cfg b/cfg/std.cfg index 79c5257a9..080c409dc 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -8342,6 +8342,12 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init std::lock_guard std::unique_lock std::shared_lock + std::fstream + std::wfstream + std::ofstream + std::wofstream + std::basic_fstream + std::basic_ofstream std::pair diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 7ee724df4..b88578b78 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1192,7 +1192,7 @@ void CheckUnusedVar::checkFunctionVariableUsage() continue; if (tok->isName()) { - if (isRaiiClass(tok->valueType(), mTokenizer->isCPP(), true)) + if (isRaiiClass(tok->valueType(), mTokenizer->isCPP(), false)) continue; tok = tok->next(); } @@ -1223,6 +1223,8 @@ void CheckUnusedVar::checkFunctionVariableUsage() op1tok = op1tok->astOperand1(); const Variable *op1Var = op1tok ? op1tok->variable() : nullptr; + if (!op1Var && Token::Match(tok, "(|{") && tok->previous() && tok->previous()->variable()) + op1Var = tok->previous()->variable(); std::string bailoutTypeName; if (op1Var) { if (op1Var->isReference() && op1Var->nameToken() != tok->astOperand1()) @@ -1278,8 +1280,8 @@ void CheckUnusedVar::checkFunctionVariableUsage() Severity::information, "checkLibraryCheckType", "--check-library: Provide configuration for " + bailoutTypeName); - continue; } + continue; } // warn diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 0000d7892..923e1c7ca 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -5431,6 +5431,17 @@ private: " std::unique_lock lock(m);\n" // #4624 "}"); ASSERT_EQUALS("", errout.str()); + + functionVariableUsage("void f() {\n" // #7732 + " const std::pair p(\"a\", \"b\");\n" + " std::pair q{\"a\", \"b\" };\n" + " auto r = std::pair(\"a\", \"b\");\n" + " auto s = std::pair{ \"a\", \"b\" };\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' is assigned a value that is never used.\n" + "[test.cpp:3]: (style) Variable 'q' is assigned a value that is never used.\n" + "[test.cpp:4]: (style) Variable 'r' is assigned a value that is never used.\n" + "[test.cpp:5]: (style) Variable 's' is assigned a value that is never used.\n", errout.str()); } void localVarClass() { @@ -5610,7 +5621,7 @@ private: " int buf[6];\n" " Data data(buf);\n" "}"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (information) --check-library: Provide configuration for Data\n", errout.str()); } void localvarCpp11Initialization() {