diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 7885eb8b4..e4973b072 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3906,6 +3906,17 @@ static bool isLiteralNumber(const Token *tok, bool cpp) return tok->isNumber() || tok->isEnumerator() || tok->str() == "NULL" || (cpp && Token::Match(tok, "false|true|nullptr")); } +static bool isVariableInit(const Token *tok) +{ + return tok->str() == "(" && + tok->isBinaryOp() && + tok->astOperand1()->variable() && + tok->astOperand1()->variable()->nameToken() == tok->astOperand1() && + tok->astOperand1()->variable()->valueType() && + tok->astOperand1()->variable()->valueType()->type >= ValueType::Type::VOID && + !Token::simpleMatch(tok->astOperand2(), ","); +} + static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings) { for (const Scope * scope : symboldatabase->functionScopes) { @@ -3918,7 +3929,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat } // Assignment - if ((tok->str() != "=") || (tok->astParent())) + if ((tok->str() != "=" && !isVariableInit(tok)) || (tok->astParent())) continue; // Lhs should be a variable diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 9fe544132..7f73eed12 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1505,6 +1505,12 @@ private: "}"; ASSERT_EQUALS(true, testValueOfX(code, 3U, 123)); + code = "void f() {\n" + " const int x(321);\n" + " a = x;\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 321)); + code = "void f() {\n" " int x = 9;\n" " --x;\n"