Partial fix for #10358: set value for int s{ 4 }; (#3506)

This commit is contained in:
chrchr-github 2021-10-15 20:05:42 +02:00 committed by GitHub
parent 84f102283b
commit c88dceff95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -4714,7 +4714,7 @@ static std::list<ValueFlow::Value> truncateValues(std::list<ValueFlow::Value> 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() &&

View File

@ -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, "&");