Fixed false positive "Variable is not assigned a value" on class types

This commit is contained in:
PKEuS 2012-07-25 00:34:27 -07:00
parent f82e5396c8
commit c11e8cdbfa
2 changed files with 5 additions and 4 deletions

View File

@ -940,6 +940,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
for (Variables::VariableMap::const_iterator it = variables.varUsage().begin(); it != variables.varUsage().end(); ++it) {
const Variables::VariableUsage &usage = it->second;
const std::string &varname = usage._name->str();
const Variable* var = symbolDatabase->getVariableFromVarId(it->first);
// variable has been marked as unused so ignore it
if (usage._name->isUnused())
@ -961,7 +962,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
unusedVariableError(usage._name, varname);
// variable has not been written but has been modified
else if (usage._modified && !usage._write && !usage._allocateMemory)
else if (usage._modified && !usage._write && !usage._allocateMemory && !Token::simpleMatch(var->typeStartToken(), "std ::"))
unassignedVariableError(usage._name, varname);
// variable has been written but not read
@ -969,7 +970,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
unreadVariableError(usage._name, varname);
// variable has been read but not written
else if (!usage._write && !usage._allocateMemory)
else if (!usage._write && !usage._allocateMemory && !Token::simpleMatch(var->typeStartToken(), "std ::"))
unassignedVariableError(usage._name, varname);
}
}

View File

@ -3087,10 +3087,10 @@ private:
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used\n", errout.str());
functionVariableUsage("std::string foo() {\n"
" std::string s;\n"
" std::string s;\n" // Class instances are initialized. Assignement is not necessary
" return s;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is not assigned a value\n", errout.str());
ASSERT_EQUALS("", errout.str());
functionVariableUsage("std::string foo() {\n"
" std::string s = \"foo\";\n"