Fixed #8223 (false negative: Statements following return, break, continue, goto or throw will never be executed.)

This commit is contained in:
Daniel Marjamäki 2021-04-04 19:43:51 +02:00
parent 518fb01553
commit 084bc74080
2 changed files with 2 additions and 3 deletions

View File

@ -809,8 +809,7 @@ void CheckOther::checkUnreachableCode()
const bool inconclusive = secondBreak && (secondBreak->linenr() - 1 > secondBreak->previous()->linenr());
if (secondBreak && (printInconclusive || !inconclusive)) {
if (Token::Match(secondBreak, "continue|goto|throw") ||
(secondBreak->str() == "return" && (tok->str() == "return" || secondBreak->strAt(1) == ";"))) { // return with value after statements like throw can be necessary to make a function compile
if (Token::Match(secondBreak, "continue|goto|throw|return")) {
duplicateBreakError(secondBreak, inconclusive);
tok = Token::findmatch(secondBreak, "[}:]");
} else if (secondBreak->str() == "break") { // break inside switch as second break statement should not issue a warning

View File

@ -3549,7 +3549,7 @@ private:
" throw 0;\n"
" return 1;\n"
"}", nullptr, false, false, false);
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary.\n", errout.str());
check("void foo() {\n"
" throw 0;\n"