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
2 changes: 2 additions & 0 deletions lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ void CheckFunctionsImpl::checkMissingReturn()
continue;
if (Function::returnsVoid(function, true))
continue;
if (Function::isCoroutine(function, mSettings.standards, *mTokenizer))
continue;
const Token *errorToken = checkMissingReturnScope(scope->bodyEnd, mSettings.library);
if (errorToken)
missingReturnError(errorToken);
Expand Down
14 changes: 14 additions & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3407,6 +3407,20 @@ bool Function::returnsVoid(const Function* function, bool unknown)
});
}

bool Function::isCoroutine(const Function* function, const Standards &standards, const Tokenizer &tokens)
{
if (!tokens.isCPP() || standards.cpp < Standards::CPP20)
return false;
if (!function->functionScope)
Comment thread
ludviggunne marked this conversation as resolved.
Dismissed
Comment thread
ludviggunne marked this conversation as resolved.
return false;
const Scope *scope = function->functionScope;
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (Token::Match(tok, "co_return|co_await|co_yield"))
return true;
}
return false;
}

std::vector<const Token*> Function::findReturns(const Function* f)
{
std::vector<const Token*> result;
Expand Down
1 change: 1 addition & 0 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ class CPPCHECKLIB Function {
static bool returnsStandardType(const Function* function, bool unknown = false);

static bool returnsVoid(const Function* function, bool unknown = false);
static bool isCoroutine(const Function* function, const Standards &standards, const Tokenizer &tokens);

static std::vector<const Token*> findReturns(const Function* f);

Expand Down
8 changes: 8 additions & 0 deletions test/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class TestFunctions : public TestFixture {
TEST_CASE(checkMissingReturn5);
TEST_CASE(checkMissingReturn6); // #13180
TEST_CASE(checkMissingReturn7); // #14370 - FN try/catch
TEST_CASE(checkMissingReturn8);
TEST_CASE(checkMissingReturnStdInt); // #14482 - FN std::int32_t

// std::move for locar variable
Expand Down Expand Up @@ -1927,6 +1928,13 @@ class TestFunctions : public TestFixture {
ASSERT_EQUALS("[test.cpp:3:19]: (error) Found an exit path from function with non-void return type that has missing return statement [missingReturn]\n", errout_str());
}

void checkMissingReturn8() {
check("boost::asio::awaitable<void> test() {\n"
" co_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