From d2284ddbcd2a70b4a39047ae32b1c5662060407f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 14 Jul 2019 16:20:45 +0200 Subject: [PATCH] Fix false positives in self check: Variable t is assigned a value that is never used. Classes with destructors was not handled properly. --- lib/checkunusedvar.cpp | 2 +- test/testunusedvar.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 6f46e7792..ba9168e02 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1131,7 +1131,7 @@ void CheckUnusedVar::checkFunctionVariableUsage() case ValueType::Type::UNKNOWN_TYPE: case ValueType::Type::NONSTD: case ValueType::Type::RECORD: - check = tok->valueType()->typeScope && tok->valueType()->typeScope->getDestructor(); + check = tok->valueType()->typeScope && !tok->valueType()->typeScope->getDestructor(); break; case ValueType::Type::CONTAINER: case ValueType::Type::ITERATOR: diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index e2f8d3790..714fc97d4 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -149,6 +149,7 @@ private: TEST_CASE(localvarthrow); // ticket #3687 TEST_CASE(localVarStd); + TEST_CASE(localVarClass); // Don't give false positives for variables in structs/unions TEST_CASE(localvarStruct1); @@ -4148,6 +4149,25 @@ private: ASSERT_EQUALS("", errout.str()); } + void localVarClass() { + functionVariableUsage("void f() {\n" + " Fred f;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + functionVariableUsage("class C { int x; };\n" + "void f() {\n" + " C c;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: c\n", errout.str()); + + functionVariableUsage("class C { public: C(int); ~C(); };\n" + "void f() {\n" + " C c(12);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + // ticket #3104 - false positive when variable is read with "if (NOT var)" void localvarIfNOT() { functionVariableUsage("void f() {\n"