Skip to content

Commit e4ecfd7

Browse files
committed
missingReturn; Fixed false positive when if condition is always true
1 parent a336c07 commit e4ecfd7

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/checkfunctions.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ static const Token *checkMissingReturnScope(const Token *tok, const Library &lib
316316
if (!hasDefault)
317317
return tok->link();
318318
} else if (tok->scope()->type == Scope::ScopeType::eIf) {
319+
const Token *condition = tok->scope()->classDef->next()->astOperand2();
320+
if (condition && condition->hasKnownIntValue() && condition->getKnownIntValue() == 1)
321+
return checkMissingReturnScope(tok, library);
319322
return tok;
320323
} else if (tok->scope()->type == Scope::ScopeType::eElse) {
321324
const Token *errorToken = checkMissingReturnScope(tok, library);

test/testfunctions.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,18 @@ class TestFunctions : public TestFixture {
14361436
"}");
14371437
ASSERT_EQUALS("[test.cpp:3]: (error) Found a exit path from function with non-void return type that has missing return statement\n", errout.str());
14381438

1439+
check("int f() {\n"
1440+
" if (!0) {\n"
1441+
" return 1;\n"
1442+
" }\n"
1443+
"}");
1444+
ASSERT_EQUALS("", errout.str());
1445+
1446+
check("int f() {\n"
1447+
" if (!0) {}\n"
1448+
"}");
1449+
ASSERT_EQUALS("[test.cpp:2]: (error) Found a exit path from function with non-void return type that has missing return statement\n", errout.str());
1450+
14391451
// loop
14401452
check("int f(int x) {\n"
14411453
" while (1) {\n"

0 commit comments

Comments
 (0)