Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 78 additions & 6 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3793,6 +3793,14 @@ static std::set<MathLib::bigint> getSwitchValues(const Token *startbrace, bool &
return values;
}

static bool coversAllEnumerators(const Scope *enumScope, const std::set<MathLib::bigint> &values)
{
const std::vector<Enumerator> &list = enumScope->enumeratorList;
return std::all_of(list.cbegin(), list.cend(), [&](const Enumerator &e) {
return !e.value_known || values.count(e.value);
});
}

bool isExhaustiveSwitch(const Token *startbrace)
{
if (!startbrace || !Token::simpleMatch(startbrace->previous(), ") {") || startbrace->scope()->type != ScopeType::eSwitch)
Expand All @@ -3813,16 +3821,80 @@ bool isExhaustiveSwitch(const Token *startbrace)
if (condition->valueType()->type == ValueType::Type::BOOL)
return switchValues.count(0) && switchValues.count(1);

if (condition->valueType()->isEnum()) {
const std::vector<Enumerator> &enumList = condition->valueType()->typeScope->enumeratorList;
return std::all_of(enumList.cbegin(), enumList.cend(), [&](const Enumerator &e) {
return !e.value_known || switchValues.count(e.value);
});
}
if (condition->valueType()->isEnum())
return coversAllEnumerators(condition->valueType()->typeScope, switchValues);

return false;
}

static const Token *getEnumeratorTok(const Token *tok)
{
while (Token::simpleMatch(tok, "::"))
tok = tok->astOperand2();
return (tok && tok->enumerator()) ? tok : nullptr;
}

static const Token *getNextElseIf(const Token *ifTok)
{
const Token *endBrace = ifTok->linkAt(1)->linkAt(1);
if (!Token::simpleMatch(endBrace, "} else { if (") || !endBrace->tokAt(2)->isInsertedBrace())
return nullptr;
return endBrace->tokAt(3);
}

static std::vector<const Token *> getIfChainConditions(const Token *ifTok)
{
std::vector<const Token *> conditions;
for (; ifTok; ifTok = getNextElseIf(ifTok))
conditions.push_back(ifTok->next()->astOperand2());
return conditions;
}

static const Token *getIfChainFinalElse(const Token *ifTok)
{
while (const Token *next = getNextElseIf(ifTok))
ifTok = next;
const Token *endBrace = ifTok->linkAt(1)->linkAt(1);
if (!Token::simpleMatch(endBrace, "} else {"))
return nullptr;
return endBrace->tokAt(2);
}

bool isExhaustiveIfChain(const Token *ifTok)
{
if (getIfChainFinalElse(ifTok))
return true;

const Token *expr = nullptr;
const Scope *enumScope = nullptr;
std::set<MathLib::bigint> values;

for (const Token *cond : getIfChainConditions(ifTok)) {
if (!Token::simpleMatch(cond, "=="))
return false;

const Token *exprTok = cond->astOperand1();
const Token *enumTok = getEnumeratorTok(cond->astOperand2());
if (!enumTok || !enumTok->enumerator()->value_known)
return false;

if (enumScope && enumScope != enumTok->enumerator()->scope)
return false;

if (exprTok->exprId() == 0 || (expr && expr->exprId() != exprTok->exprId()))
return false;

expr = exprTok;
enumScope = enumTok->enumerator()->scope;
values.insert(enumTok->enumerator()->value);
}

if (expr->valueType() && expr->valueType()->typeScope != enumScope)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

If expr has no value type, this check passes and the chain counts as exhaustive. Then a non-enum or unknown-typed expression matched against all enumerators suppresses missingReturn. Base warned for both of these, the PR does not:

enum E { E1, E2 };
template<int t> int f9() {
    if constexpr (t == E1) return 1;
    else if constexpr (t == E2) return 2;
}
int f10(Unknown u) {
    if (u == E1) return 1;
    else if (u == E2) return 2;
}

The same happens for s->x where S is incomplete. It may be better to accept a missing value type only for a non-type template parameter whose declared type is the enum, and reject it otherwise.

return false;

return coversAllEnumerators(enumScope, values);
}

bool isUnreachableOperand(const Token *tok)
{
for (;;)
Expand Down
2 changes: 2 additions & 0 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ bool isUnevaluated(const Token *tok);

bool isExhaustiveSwitch(const Token *startbrace);

bool isExhaustiveIfChain(const Token *ifTok);

bool isUnreachableOperand(const Token *tok);

const Token *skipUnreachableBranch(const Token *tok);
Expand Down
12 changes: 12 additions & 0 deletions lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,20 @@ static const Token *checkMissingReturnScope(const Token *tok, const Library &lib
const Token *condition = paren->astOperand2();
if (condition && condition->hasKnownIntValue() && condition->getKnownIntValue() == 1)
return checkMissingReturnScope(tok, library);
const Token *ifTok = paren->previous();
while (Token::simpleMatch(ifTok->tokAt(-3), "} else {") && ifTok->previous()->isInsertedBrace())
ifTok = ifTok->linkAt(-3)->linkAt(-1)->previous();
if (isExhaustiveIfChain(ifTok))
return checkMissingReturnScope(tok, library);
return tok;
} else if (tok->scope()->type == ScopeType::eElse) {
const Token *ifEnd = tok->link()->tokAt(-2);
if (Token::simpleMatch(ifEnd, "} else {")) {
// else branch is unreachable if condition is always true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This new handling of a known-true condition in front of an else changes results on its own. For example int f() { const int x = 1; if (x) { return 1; } else { } } no longer warns. checkMissingReturn10 has no test for it. Please add a test for it (and one where the if body lacks the return).

const Token *condition = ifEnd->link()->linkAt(-1)->astOperand2();
if (condition && condition->hasKnownIntValue() && condition->getKnownIntValue() != 0)
return checkMissingReturnScope(ifEnd, library);
}
const Token *errorToken = checkMissingReturnScope(tok, library);
if (errorToken)
return errorToken;
Expand Down
58 changes: 58 additions & 0 deletions test/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class TestFunctions : public TestFixture {
TEST_CASE(checkMissingReturn7); // #14370 - FN try/catch
TEST_CASE(checkMissingReturn8);
TEST_CASE(checkMissingReturn9);
TEST_CASE(checkMissingReturn10); // #12551
TEST_CASE(checkMissingReturnStdInt); // #14482 - FN std::int32_t

// std::move for locar variable
Expand Down Expand Up @@ -1964,6 +1965,63 @@ class TestFunctions : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void checkMissingReturn10() { // #12551
check("enum class E { E1, E2 };\n"
"template <E type>\n"
"static bool f() {\n"
" if constexpr (type == E::E1) {\n"
" return true;\n"
" }\n"
" else if constexpr (type == E::E2) {\n"
" return false;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("enum class E { E1, E2 };\n"
"template <E type>\n"
"static bool f() {\n"
" if constexpr (type == E::E1) {\n"
" return true;\n"
" }\n"
" else if constexpr (type == E::E2) {\n"
" return false;\n"
" }\n"
"}\n"
"int main() {\n"
" return f<E::E1>();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("enum E { E1, E2, E3 };\n"
"bool f(E e) {\n"
" if (e == E1)\n"
" return true;\n"
" else if (e == E2)\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:21]: (error) Found an exit path from function with non-void return type that has missing return statement [missingReturn]\n", errout_str());

check("enum E { E1, E2 };\n"
"bool f(E e, E g) {\n"
" if (e == E1)\n"
" return true;\n"
" else if (g == E2)\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:21]: (error) Found an exit path from function with non-void return type that has missing return statement [missingReturn]\n", errout_str());

check("enum E { E1, E2 };\n"
"namespace ns { int x; }\n"
"bool f() {\n"
" if (ns::x == E1)\n"
" return true;\n"
" else if (ns::x == E2)\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7:21]: (error) Found an exit path from function with non-void return type that has missing return statement [missingReturn]\n", errout_str());
}

void checkMissingReturnStdInt() {// #14482 - FN
check("std::int32_t f() {}\n");
ASSERT_EQUALS("[test.cpp:1:19]: (error) Found an exit path from function with non-void return type that has missing return statement [missingReturn]\n", errout_str());
Expand Down
Loading