diff --git a/lib/astutils.cpp b/lib/astutils.cpp index fd820d0cd..69f685b35 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1084,17 +1084,15 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const if (Token::Match(tok, "return|throw")) { // TODO: Handle these better // Is expr variable used in expression? - bool read = false; - visitAstNodes(tok->astOperand1(), - [&](const Token *tok2) { + const Token *end = tok->findExpressionStartEndTokens().second->next(); + for (const Token *tok2 = tok; tok2 != end; tok2 = tok2->next()) { if (!local && Token::Match(tok2, "%name% (")) - read = true; + return Result(Result::Type::READ); if (tok2->varId() && exprVarIds.find(tok2->varId()) != exprVarIds.end()) - read = true; - return read ? ChildrenToVisit::done : ChildrenToVisit::op1_and_op2; - }); + return Result(Result::Type::READ); + } - return Result(read ? Result::Type::READ : Result::Type::RETURN); + return Result(Result::Type::RETURN); } if (tok->str() == "}") { diff --git a/lib/token.cpp b/lib/token.cpp index 02ccd86d0..8d436dc0e 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1215,6 +1215,8 @@ std::pair Token::findExpressionStartEndTokens() co start = goToLeftParenthesis(start, end); end = goToRightParenthesis(start, end); + if (Token::simpleMatch(end, "{")) + end = end->link(); return std::pair(start,end); } diff --git a/test/testtoken.cpp b/test/testtoken.cpp index fa0372924..e44b5c7a4 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -939,6 +939,9 @@ private: givenACodeSampleToTokenize var2("typedef unsigned long long u64; void f() { *((u64 *)x) = 0; }"); const Token *const tok2 = Token::findsimplematch(var2.tokens(), "*"); ASSERT_EQUALS("*((unsigned long long*)x)", tok2->expressionString()); + + givenACodeSampleToTokenize data3("return (t){1,2};"); + ASSERT_EQUALS("return(t){1,2}", data3.tokens()->expressionString()); } }; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index ae9ec8107..eb42ce20e 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -107,6 +107,7 @@ private: TEST_CASE(localvar51); // ticket #8128 - FN : tok = tok->next(); TEST_CASE(localvar52); TEST_CASE(localvar53); // continue + TEST_CASE(localvar54); // ast, {} TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 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()); } + void localvar54() { + functionVariableUsage("Padding fun() {\n" + " Distance d = DISTANCE;\n" + " return (Padding){ d, d, d, d };\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void localvaralias1() { functionVariableUsage("void foo()\n" "{\n"