Fix issue 8841: FP knownConditionTrueFalse - Dont treat unknown function as noreturn (#1474)

* Fix issue 8841: Dont treat unknown function as noreturn

* Add a parameter to set default value when the function is unknown
This commit is contained in:
Paul Fultz II 2018-11-11 01:04:01 -06:00 committed by Daniel Marjamäki
parent ee2dfb6604
commit e46691597e
2 changed files with 18 additions and 4 deletions

View File

@ -304,13 +304,17 @@ static const Token * skipValueInConditionalExpression(const Token * const valuet
return nullptr;
}
static bool isEscapeScope(const Token* tok, TokenList * tokenlist)
static bool isEscapeScope(const Token* tok, TokenList * tokenlist, bool unknown = false)
{
if (!Token::simpleMatch(tok, "{"))
return false;
const Token * termTok = Token::findmatch(tok, "return|continue|break|throw|goto", tok->link());
return (termTok && termTok->scope() == tok->scope()) ||
(tokenlist && tokenlist->getSettings()->library.isScopeNoReturn(tok->link(), nullptr));
if(termTok && termTok->scope() == tok->scope())
return true;
std::string unknownFunction;
if(tokenlist && tokenlist->getSettings()->library.isScopeNoReturn(tok->link(), &unknownFunction))
return unknownFunction.empty() || unknown;
return false;
}
static bool bailoutSelfAssignment(const Token * const tok)
@ -4227,7 +4231,7 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
valueFlowContainerReverse(scope.classDef, tok->varId(), value, settings);
// possible value after condition
if (!isEscapeScope(scope.bodyStart, tokenlist)) {
if (!isEscapeScope(scope.bodyStart, tokenlist, true)) {
const Token *after = scope.bodyEnd;
if (Token::simpleMatch(after, "} else {"))
after = isEscapeScope(after->tokAt(2), tokenlist) ? nullptr : after->linkAt(2);

View File

@ -2683,6 +2683,16 @@ private:
" return;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo(int i) {\n"
" if (i==42)\n"
" {\n"
" bar();\n"
" }\n"
" if (cond && (42==i))\n"
" return;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void multiConditionAlwaysTrue() {