(Fixed #7895) C++11 brace initialization. Added test. (#862)

Add an optional extended description…
This commit is contained in:
umanamente 2017-01-22 14:16:40 +05:00 committed by PKEuS
parent 3022d74801
commit 0c1ba8e8b4
2 changed files with 34 additions and 6 deletions

View File

@ -730,13 +730,29 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
if (i->isArray() && Token::Match(i->nameToken(), "%name% [ %var% ]")) // Array index variable read.
variables.read(i->nameToken()->tokAt(2)->varId(), i->nameToken());
if (defValTok && defValTok->str() == "=") {
if (defValTok->next() && defValTok->next()->str() == "{") {
for (const Token* tok = defValTok; tok && tok != defValTok->linkAt(1); tok = tok->next())
if (tok->varId()) // Variables used to initialize the array read.
variables.read(tok->varId(), i->nameToken());
} else
if (defValTok && defValTok->next()) {
// simple assignment "var = 123"
if (defValTok->str() == "=" && defValTok->next()->str() != "{") {
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() == "{") {
// "var = {...}"
tokBraceStart = defValTok->next();
} else if (defValTok->str() == "{") {
// "var{...}"
tokBraceStart = defValTok;
}
if (tokBraceStart) {
for (const Token* tok = tokBraceStart->next(); tok && tok != tokBraceStart->link(); tok = tok->next()) {
if (tok->varId()) {
// Variables used to initialize the array read.
variables.read(tok->varId(), i->nameToken());
}
}
}
}
}
}
}

View File

@ -175,6 +175,7 @@ private:
TEST_CASE(lambdaFunction); // #5078
TEST_CASE(namespaces); // #7557
TEST_CASE(bracesInitCpp11);// #7895 - "int var{123}" initialization
}
void checkStructMemberUsage(const char code[]) {
@ -4101,6 +4102,17 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
}
void bracesInitCpp11() {
functionVariableUsage(
"int fun() {\n"
" static int fpUnread{0};\n"
" const int var{fpUnread++};\n"
" return var;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestUnusedVar)