Fixed #10523 (FP: missingReturn with nested switch statements)

This commit is contained in:
Daniel Marjamäki 2021-10-31 14:47:52 +01:00
parent bd63882d80
commit 3e6540c4b3
2 changed files with 22 additions and 4 deletions

View File

@ -303,9 +303,13 @@ static const Token *checkMissingReturnScope(const Token *tok, const Library &lib
// find reachable break / !default
bool hasDefault = false;
bool reachable = false;
for (const Token *switchToken = tok->link(); switchToken != tok; switchToken = switchToken->next()) {
if (reachable && Token::simpleMatch(switchToken, "break ;"))
return switchToken;
for (const Token *switchToken = tok->link()->next(); switchToken != tok; switchToken = switchToken->next()) {
if (reachable && Token::simpleMatch(switchToken, "break ;")) {
if (Token::simpleMatch(switchToken->previous(), "}") && !checkMissingReturnScope(switchToken->previous(), library))
reachable = false;
else
return switchToken;
}
if (switchToken->isKeyword() && Token::Match(switchToken, "return|throw"))
reachable = false;
if (Token::Match(switchToken, "%name% (") && library.isnoreturn(switchToken))
@ -314,7 +318,7 @@ static const Token *checkMissingReturnScope(const Token *tok, const Library &lib
reachable = true;
if (Token::simpleMatch(switchToken, "default :"))
hasDefault = true;
else if (switchToken->str() == "{" && switchToken->scope()->isLoopScope())
else if (switchToken->str() == "{" && (switchToken->scope()->isLoopScope() || switchToken->scope()->type == Scope::ScopeType::eSwitch))
switchToken = switchToken->link();
}
if (!hasDefault)

View File

@ -1441,6 +1441,20 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("bool test(unsigned char v1, int v2) {\n"
" switch (v1) {\n"
" case 0:\n"
" switch (v2) {\n"
" case 48000:\n"
" break;\n"
" }\n"
" return false;\n"
" default:\n"
" return true;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
// if/else
check("int f(int x) {\n"
" if (x) {\n"