Fixed #6315 (false positive - unreadVariable - variable is used via pointer)

This commit is contained in:
Daniel Marjamäki 2017-07-23 23:32:14 +02:00
parent 3d7265309b
commit 0a03bbb320
2 changed files with 19 additions and 2 deletions

View File

@ -770,8 +770,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
doAssignment(variables, i->nameToken(), false, scope);
} else {
// could be "var = {...}" OR "var{...}" (since C++11)
const Token* tokBraceStart = NULL;
if (defValTok->str() == "=" && defValTok->next()->str() == "{") {
const Token* tokBraceStart = nullptr;
if (Token::simpleMatch(defValTok, "= {")) {
// "var = {...}"
tokBraceStart = defValTok->next();
} else if (defValTok->str() == "{") {
@ -902,6 +902,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
tok = tok->next();
if (!var->isReference())
variables.read(tok->varId(), tok);
} else if (tok->str() == "[" && Token::simpleMatch(skipBrackets(tok),"= {")) {
const Token * const rhs1 = skipBrackets(tok)->next();
for (const Token *rhs = rhs1->link(); rhs != rhs1; rhs = rhs->previous()) {
if (rhs->varId())
variables.readAll(rhs->varId(), rhs);
}
} else if (var->typeEndToken()->str() == ">") // Be careful with types like std::vector
tok = tok->previous();
break;

View File

@ -117,6 +117,7 @@ private:
TEST_CASE(localvaralias12); // ticket #4394
TEST_CASE(localvaralias13); // ticket #4487
TEST_CASE(localvaralias14); // ticket #5619
TEST_CASE(localvaralias15); // ticket #6315
TEST_CASE(localvarasm);
TEST_CASE(localvarstatic);
TEST_CASE(localvarextern);
@ -3017,6 +3018,16 @@ private:
TODO_ASSERT_EQUALS("p is assigned a value that is never used", "", errout.str());
}
void localvaralias15() { // #6315
functionVariableUsage("void f() {\n"
" int x=3;\n"
" int *p = &x;\n"
" int *p2[1] = {p};\n"
" dostuff(p2);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void localvarasm() {
functionVariableUsage("void foo(int &b)\n"
"{\n"