From 0a6ea03dff058f19e46e5100ba1d92a79ff31c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 30 Nov 2026 22:39:06 +0100 Subject: [PATCH 1/2] test --- test/testfunctions.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 58228b58b07..c2050bcbf6b 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -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 @@ -1964,6 +1965,63 @@ class TestFunctions : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void checkMissingReturn10() { // #12551 + check("enum class E { E1, E2 };\n" + "template \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 \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();\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()); From 926d2fedd088305f4a984b99737db3d735131a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 30 Nov 2026 22:38:51 +0100 Subject: [PATCH 2/2] fix --- lib/astutils.cpp | 84 +++++++++++++++++++++++++++++++++++++++--- lib/astutils.h | 2 + lib/checkfunctions.cpp | 12 ++++++ 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index a08d86a2f86..e2588ee99be 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -3793,6 +3793,14 @@ static std::set getSwitchValues(const Token *startbrace, bool & return values; } +static bool coversAllEnumerators(const Scope *enumScope, const std::set &values) +{ + const std::vector &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) @@ -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 &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 getIfChainConditions(const Token *ifTok) +{ + std::vector 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 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) + return false; + + return coversAllEnumerators(enumScope, values); +} + bool isUnreachableOperand(const Token *tok) { for (;;) diff --git a/lib/astutils.h b/lib/astutils.h index 578763124ac..e277c64f3a1 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -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); diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 89094b6bf4c..60fb1e5fbd7 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -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 + 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;