Fixed #4839 (Variable (in array) is assigned a value that is never used)

This commit is contained in:
Daniel Marjamäki 2016-05-17 16:03:55 +02:00
parent 41576ee8ec
commit 7453b641bd
2 changed files with 18 additions and 1 deletions

View File

@ -1013,6 +1013,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
// assignment // assignment
else if ((Token::Match(tok, "%name% [") && Token::simpleMatch(skipBracketsAndMembers(tok->next()), "=")) || else if ((Token::Match(tok, "%name% [") && Token::simpleMatch(skipBracketsAndMembers(tok->next()), "=")) ||
(Token::simpleMatch(tok, "* (") && Token::simpleMatch(tok->next()->link(), ") ="))) { (Token::simpleMatch(tok, "* (") && Token::simpleMatch(tok->next()->link(), ") ="))) {
const Token *eq = tok;
while (eq && !eq->isAssignmentOp())
eq = eq->astParent();
const bool deref = eq && eq->astOperand1() && eq->astOperand1()->valueType() && eq->astOperand1()->valueType()->pointer == 0U;
if (tok->str() == "*") { if (tok->str() == "*") {
tok = tok->tokAt(2); tok = tok->tokAt(2);
if (tok->str() == "(") if (tok->str() == "(")
@ -1031,7 +1037,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.read(varid, tok); variables.read(varid, tok);
variables.writeAliases(varid, tok); variables.writeAliases(varid, tok);
} else if (var->_type == Variables::pointerArray) { } else if (var->_type == Variables::pointerArray) {
tok = doAssignment(variables, tok, false, scope); tok = doAssignment(variables, tok, deref, scope);
} else } else
variables.writeAll(varid, tok); variables.writeAll(varid, tok);
} }

View File

@ -119,6 +119,7 @@ private:
TEST_CASE(localvararray1); // ticket #2780 TEST_CASE(localvararray1); // ticket #2780
TEST_CASE(localvararray2); // ticket #3438 TEST_CASE(localvararray2); // ticket #3438
TEST_CASE(localvararray3); // ticket #3980 TEST_CASE(localvararray3); // ticket #3980
TEST_CASE(localvararray4); // ticket #4839
TEST_CASE(localvarstring1); TEST_CASE(localvarstring1);
TEST_CASE(localvarstring2); // ticket #2929 TEST_CASE(localvarstring2); // ticket #2929
TEST_CASE(localvarconst1); TEST_CASE(localvarconst1);
@ -3654,6 +3655,16 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void localvararray4() {
functionVariableUsage("void foo() {\n"
" int p[1];\n"
" int *pp[0];\n"
" p[0] = 1;\n"
" *pp[0] = p[0];\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void localvarstring1() { // ticket #1597 void localvarstring1() { // ticket #1597
functionVariableUsage("void foo() {\n" functionVariableUsage("void foo() {\n"
" std::string s;\n" " std::string s;\n"