diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 1edfdfab6ac..1f17d0bbcdc 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -386,7 +386,12 @@ static const Token *checkMissingReturnScope(const Token *tok, const Library &lib if (!isExhaustiveSwitch(tok->link())) return tok->link(); } else if (tok->scope()->type == ScopeType::eIf) { - const Token *condition = tok->scope()->classDef->next()->astOperand2(); + const Token *paren = tok->link()->linkAt(-1); + if (!paren || !Token::simpleMatch(paren->astOperand1(), "if")) { + tok = tok->link(); + continue; + } + const Token *condition = paren->astOperand2(); if (condition && condition->hasKnownIntValue() && condition->getKnownIntValue() == 1) return checkMissingReturnScope(tok, library); return tok; diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index f5067bc31dc..d5a18f7d916 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -87,6 +87,7 @@ class TestFunctions : public TestFixture { TEST_CASE(checkMissingReturn6); // #13180 TEST_CASE(checkMissingReturn7); // #14370 - FN try/catch TEST_CASE(checkMissingReturn8); + TEST_CASE(checkMissingReturn9); TEST_CASE(checkMissingReturnStdInt); // #14482 - FN std::int32_t // std::move for locar variable @@ -1936,6 +1937,16 @@ class TestFunctions : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void checkMissingReturn9() { + check("struct S { int v; };\n" + " S operator/(S x, S y) { return { x.v / y.v }; }\n" + " S f(int a, int b) {\n" + " if (b) { return S{ a } / S{ b }; }\n" + " else { return {}; }\n" + "}\n"); + ASSERT_EQUALS("", 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());