Fix #11417 FP knownConditionTrueFalse with container and brace init (#4606)

* Fix #11417 FP knownConditionTrueFalse with container and brace init

* Format

* Format

* Move to getInitListSize()
This commit is contained in:
chrchr-github 2022-12-02 22:07:08 +01:00 committed by GitHub
parent e4ee7cd59c
commit 7d6683fb78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -8085,6 +8085,15 @@ static std::vector<ValueFlow::Value> getContainerSizeFromConstructorArgs(const s
return {};
}
static bool valueFlowIsSameContainerType(const ValueType& contType, const Token* tok, const Settings* settings)
{
if (!tok || !tok->valueType() || !tok->valueType()->containerTypeToken)
return false;
const ValueType tokType = ValueType::parseDecl(tok->valueType()->containerTypeToken, settings, true);
return contType.isTypeEqual(&tokType);
}
static std::vector<ValueFlow::Value> getInitListSize(const Token* tok,
const ValueType* valueType,
const Settings* settings,
@ -8108,6 +8117,8 @@ static std::vector<ValueFlow::Value> getInitListSize(const Token* tok,
initList = true;
else if (vt.isIntegral() && astIsIntegral(args[0], false))
initList = true;
else if (args.size() == 1 && valueFlowIsSameContainerType(vt, tok->astOperand2(), settings))
initList = false; // copy ctor
}
}
if (!initList)

View File

@ -6317,6 +6317,22 @@ private:
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 5U, 0));
code = "std::vector<int> g();\n" // #11417
"int f() {\n"
" std::vector<int> v{ g() };\n"
" auto x = v.size();\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXKnown(code, 5U, 1));
code = "std::vector<int> g();\n"
"int f() {\n"
" std::vector<std::vector<int>> v{ g() };\n"
" auto x = v.size();\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 1));
}
void valueFlowContainerElement()