Fixed #8128 (False negative: variable is assigned value that is not read)

This commit is contained in:
Daniel Marjamäki 2017-08-01 14:56:53 +02:00
parent 7ab8d758c5
commit 9ef9d0e4a8
2 changed files with 18 additions and 1 deletions

View File

@ -1038,7 +1038,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
else
variables.write(varid1, tok);
} else if (varid1 && Token::Match(tok, "%varid% .", varid1)) {
variables.use(varid1, tok);
variables.read(varid1, tok);
variables.write(varid1, start);
} else if (var &&
var->_type == Variables::pointer &&
Token::Match(tok, "%name% ;") &&

View File

@ -103,6 +103,7 @@ private:
TEST_CASE(localvar48); // ticket #6954
TEST_CASE(localvar49); // ticket #7594
TEST_CASE(localvar50); // ticket #6261 : dostuff(cond ? buf1 : buf2)
TEST_CASE(localvar51); // ticket #8128 - FN : tok = tok->next();
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
TEST_CASE(localvaralias3); // ticket #1639
@ -2044,6 +2045,21 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvar51() { // #8128 FN
functionVariableUsage("void foo() {\n"
" const char *tok = var->nameToken();\n"
" tok = tok->next();\n" // read+write
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'tok' is assigned a value that is never used.\n", errout.str());
// TODO: False negative
functionVariableUsage("void foo() {\n"
" int x = 4;\n"
" x = 15 + x;\n" // read+write
"}");
TODO_ASSERT_EQUALS("error", "", errout.str());
}
void localvaralias1() {
functionVariableUsage("void foo()\n"
"{\n"