From c88dceff95d70726dc0ac46fe4ec811492d8ba4d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 15 Oct 2021 20:05:42 +0200 Subject: [PATCH] Partial fix for #10358: set value for int s{ 4 }; (#3506) --- lib/valueflow.cpp | 2 +- test/testvalueflow.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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, "&");