Fixed #8933 (false negative: (style) Variable is assigned a value that is never used (std::string))

This commit is contained in:
Daniel Marjamäki 2019-07-14 09:44:30 +02:00
parent cf06acae08
commit 1cadd9398a
2 changed files with 32 additions and 8 deletions

View File

@ -1122,8 +1122,37 @@ void CheckUnusedVar::checkFunctionVariableUsage()
if ((!tok->isAssignmentOp() || !tok->astOperand1()) && !(Token::Match(tok, "%var% (") && tok->variable() && tok->variable()->nameToken() == tok))
continue;
if (tok->isName()) {
if (!tok->valueType() || !tok->valueType()->isIntegral())
continue;
if (mTokenizer->isCPP()) {
// do not check RAII/scope_lock objects
if (!tok->valueType())
continue;
bool check = false;
switch(tok->valueType()->type) {
case ValueType::Type::UNKNOWN_TYPE:
case ValueType::Type::NONSTD:
case ValueType::Type::RECORD:
check = tok->valueType()->typeScope && tok->valueType()->typeScope->getDestructor();
break;
case ValueType::Type::CONTAINER:
case ValueType::Type::ITERATOR:
case ValueType::Type::VOID:
case ValueType::Type::BOOL:
case ValueType::Type::CHAR:
case ValueType::Type::SHORT:
case ValueType::Type::WCHAR_T:
case ValueType::Type::INT:
case ValueType::Type::LONG:
case ValueType::Type::LONGLONG:
case ValueType::Type::UNKNOWN_INT:
case ValueType::Type::FLOAT:
case ValueType::Type::DOUBLE:
case ValueType::Type::LONGDOUBLE:
check = true;
break;
};
if (!check)
continue;
}
tok = tok->next();
}
if (tok->astParent() && tok->str() != "(") {

View File

@ -4124,18 +4124,13 @@ private:
functionVariableUsage("void f() {\n"
" std::vector<int> x(100);\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'x' is assigned a value that is never used.\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void f() {\n"
" std::vector<MyClass> x;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: x\n", errout.str());
functionVariableUsage("void f() {\n"
" std::vector<MyClass> x(100);\n" // Might have a side-effect
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void f() {\n"
" std::lock_guard<MyClass> lock(mutex_);\n" // Has a side-effect #4385
"}");