Unused value: Fix false positive (ast, {})

This commit is contained in:
Daniel Marjamäki 2018-12-14 18:56:09 +01:00
parent be6782d386
commit 37416010ef
4 changed files with 20 additions and 8 deletions

View File

@ -1084,17 +1084,15 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
if (Token::Match(tok, "return|throw")) { if (Token::Match(tok, "return|throw")) {
// TODO: Handle these better // TODO: Handle these better
// Is expr variable used in expression? // Is expr variable used in expression?
bool read = false; const Token *end = tok->findExpressionStartEndTokens().second->next();
visitAstNodes(tok->astOperand1(), for (const Token *tok2 = tok; tok2 != end; tok2 = tok2->next()) {
[&](const Token *tok2) {
if (!local && Token::Match(tok2, "%name% (")) if (!local && Token::Match(tok2, "%name% ("))
read = true; return Result(Result::Type::READ);
if (tok2->varId() && exprVarIds.find(tok2->varId()) != exprVarIds.end()) if (tok2->varId() && exprVarIds.find(tok2->varId()) != exprVarIds.end())
read = true; return Result(Result::Type::READ);
return read ? ChildrenToVisit::done : ChildrenToVisit::op1_and_op2; }
});
return Result(read ? Result::Type::READ : Result::Type::RETURN); return Result(Result::Type::RETURN);
} }
if (tok->str() == "}") { if (tok->str() == "}") {

View File

@ -1215,6 +1215,8 @@ std::pair<const Token *, const Token *> Token::findExpressionStartEndTokens() co
start = goToLeftParenthesis(start, end); start = goToLeftParenthesis(start, end);
end = goToRightParenthesis(start, end); end = goToRightParenthesis(start, end);
if (Token::simpleMatch(end, "{"))
end = end->link();
return std::pair<const Token *, const Token *>(start,end); return std::pair<const Token *, const Token *>(start,end);
} }

View File

@ -939,6 +939,9 @@ private:
givenACodeSampleToTokenize var2("typedef unsigned long long u64; void f() { *((u64 *)x) = 0; }"); givenACodeSampleToTokenize var2("typedef unsigned long long u64; void f() { *((u64 *)x) = 0; }");
const Token *const tok2 = Token::findsimplematch(var2.tokens(), "*"); const Token *const tok2 = Token::findsimplematch(var2.tokens(), "*");
ASSERT_EQUALS("*((unsigned long long*)x)", tok2->expressionString()); ASSERT_EQUALS("*((unsigned long long*)x)", tok2->expressionString());
givenACodeSampleToTokenize data3("return (t){1,2};");
ASSERT_EQUALS("return(t){1,2}", data3.tokens()->expressionString());
} }
}; };

View File

@ -107,6 +107,7 @@ private:
TEST_CASE(localvar51); // ticket #8128 - FN : tok = tok->next(); TEST_CASE(localvar51); // ticket #8128 - FN : tok = tok->next();
TEST_CASE(localvar52); TEST_CASE(localvar52);
TEST_CASE(localvar53); // continue TEST_CASE(localvar53); // continue
TEST_CASE(localvar54); // ast, {}
TEST_CASE(localvaralias1); TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637 TEST_CASE(localvaralias2); // ticket #1637
TEST_CASE(localvaralias3); // ticket #1639 TEST_CASE(localvaralias3); // ticket #1639
@ -2109,6 +2110,14 @@ private:
// TODO ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str()); // TODO ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str());
} }
void localvar54() {
functionVariableUsage("Padding fun() {\n"
" Distance d = DISTANCE;\n"
" return (Padding){ d, d, d, d };\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvaralias1() { void localvaralias1() {
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"