Skip to content

Commit 1444269

Browse files
authored
Fix #14962: False Positive: missingReturn: return expression misdiagnosed as missing return path (#8780)
1 parent dc9bd7f commit 1444269

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

lib/checkfunctions.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,12 @@ static const Token *checkMissingReturnScope(const Token *tok, const Library &lib
386386
if (!isExhaustiveSwitch(tok->link()))
387387
return tok->link();
388388
} else if (tok->scope()->type == ScopeType::eIf) {
389-
const Token *condition = tok->scope()->classDef->next()->astOperand2();
389+
const Token *paren = tok->link()->linkAt(-1);
390+
if (!paren || !Token::simpleMatch(paren->astOperand1(), "if")) {
391+
tok = tok->link();
392+
continue;
393+
}
394+
const Token *condition = paren->astOperand2();
390395
if (condition && condition->hasKnownIntValue() && condition->getKnownIntValue() == 1)
391396
return checkMissingReturnScope(tok, library);
392397
return tok;

test/testfunctions.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class TestFunctions : public TestFixture {
8787
TEST_CASE(checkMissingReturn6); // #13180
8888
TEST_CASE(checkMissingReturn7); // #14370 - FN try/catch
8989
TEST_CASE(checkMissingReturn8);
90+
TEST_CASE(checkMissingReturn9);
9091
TEST_CASE(checkMissingReturnStdInt); // #14482 - FN std::int32_t
9192

9293
// std::move for locar variable
@@ -1936,6 +1937,16 @@ class TestFunctions : public TestFixture {
19361937
ASSERT_EQUALS("", errout_str());
19371938
}
19381939

1940+
void checkMissingReturn9() {
1941+
check("struct S { int v; };\n"
1942+
" S operator/(S x, S y) { return { x.v / y.v }; }\n"
1943+
" S f(int a, int b) {\n"
1944+
" if (b) { return S{ a } / S{ b }; }\n"
1945+
" else { return {}; }\n"
1946+
"}\n");
1947+
ASSERT_EQUALS("", errout_str());
1948+
}
1949+
19391950
void checkMissingReturnStdInt() {// #14482 - FN
19401951
check("std::int32_t f() {}\n");
19411952
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());

0 commit comments

Comments
 (0)