diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ceab53bb4..c0dae5b33 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4714,7 +4714,7 @@ static std::list truncateValues(std::list va static bool isVariableInit(const Token *tok) { - return tok->str() == "(" && + return (tok->str() == "(" || tok->str() == "{") && tok->isBinaryOp() && tok->astOperand1()->variable() && tok->astOperand1()->variable()->nameToken() == tok->astOperand1() && diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index bc5cf9800..477bf0f03 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -4296,6 +4296,22 @@ private: ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); // value of x can be 1 ASSERT_EQUALS(false, testValueOfX(code, 3U, 2)); // value of x can't be 2 + code = "bool f() {\n" + " const int s( 4 );" + " return s == 4;\n" // <- known value + "}"; + value = valueOfTok(code, "=="); + ASSERT(value.isKnown()); + ASSERT_EQUALS(1, value.intvalue); + + code = "bool f() {\n" + " const int s{ 4 };" + " return s == 4;\n" // <- known value + "}"; + value = valueOfTok(code, "=="); + ASSERT(value.isKnown()); + ASSERT_EQUALS(1, value.intvalue); + // calculation with known result code = "int f(int x) { a = x & 0; }"; // <- & is 0 value = valueOfTok(code, "&");