missingReturn; Fixed false positive when if condition is always true

This commit is contained in:
Daniel Marjamäki 2021-07-12 17:53:32 +02:00
parent a336c07663
commit e4ecfd7be8
2 changed files with 15 additions and 0 deletions

View File

@ -316,6 +316,9 @@ static const Token *checkMissingReturnScope(const Token *tok, const Library &lib
if (!hasDefault)
return tok->link();
} else if (tok->scope()->type == Scope::ScopeType::eIf) {
const Token *condition = tok->scope()->classDef->next()->astOperand2();
if (condition && condition->hasKnownIntValue() && condition->getKnownIntValue() == 1)
return checkMissingReturnScope(tok, library);
return tok;
} else if (tok->scope()->type == Scope::ScopeType::eElse) {
const Token *errorToken = checkMissingReturnScope(tok, library);

View File

@ -1436,6 +1436,18 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Found a exit path from function with non-void return type that has missing return statement\n", errout.str());
check("int f() {\n"
" if (!0) {\n"
" return 1;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int f() {\n"
" if (!0) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Found a exit path from function with non-void return type that has missing return statement\n", errout.str());
// loop
check("int f(int x) {\n"
" while (1) {\n"