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
7 changes: 6 additions & 1 deletion lib/checkfunctions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
Expand Down
11 changes: 11 additions & 0 deletions test/testfunctions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand DownExpand Up@@ -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());
Expand Down
Loading