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"