Skip to content

Commit

Permalink
#13180: Improve check: missing return not found (#6874)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludviggunne authored Oct 7, 2024
1 parent b56ce53 commit fb994f9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,10 @@ static const Token *checkMissingReturnScope(const Token *tok, const Library &lib
return nullptr;
if (tok->str() == "goto" && !isForwardJump(tok))
return nullptr;
if (Token::Match(tok, "%name% (") && !library.isnotnoreturn(tok)) {
if (Token::Match(tok, "%name% (") && library.isnoreturn(tok)) {
return nullptr;
}
if (Token::Match(tok->previous(), "[;{}] %name% (") && !library.isnotnoreturn(tok)) {
return nullptr;
}
if (Token::Match(tok, "[;{}] %name% :"))
Expand Down
11 changes: 10 additions & 1 deletion test/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class TestFunctions : public TestFixture {
TEST_CASE(checkMissingReturn3);
TEST_CASE(checkMissingReturn4);
TEST_CASE(checkMissingReturn5);
TEST_CASE(checkMissingReturn6); // #13180

// std::move for locar variable
TEST_CASE(returnLocalStdMove1);
Expand Down Expand Up @@ -1777,7 +1778,7 @@ class TestFunctions : public TestFixture {
" return 2;\n"
" }\n"
"}\n"
"int _tmain(int argc, _TCHAR* argv[])\n"
"int main(int argc, char* argv[])\n"
"{\n"
" auto const b= f(true);\n"
" auto const c= f(false);\n"
Expand Down Expand Up @@ -1845,6 +1846,14 @@ class TestFunctions : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void checkMissingReturn6() {// #13180
check("int foo(void)\n"
"{\n"
" i = readData();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
}

// NRVO check
void returnLocalStdMove1() {
check("struct A{}; A f() { A var; return std::move(var); }");
Expand Down

0 comments on commit fb994f9

Please sign in to comment.