Fix false positives in self check: Variable t is assigned a value that is never used. Classes with destructors was not handled properly.

This commit is contained in:
Daniel Marjamäki 2019-07-14 16:20:45 +02:00
parent 534659e596
commit d2284ddbcd
2 changed files with 21 additions and 1 deletions

View File

@ -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:

View File

@ -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"