Unreachable code; better handling of throw in C code

This commit is contained in:
Daniel Marjamäki 2021-04-05 04:53:07 +02:00
parent a21c81365d
commit cf8a423636
2 changed files with 8 additions and 2 deletions

View File

@ -784,7 +784,7 @@ void CheckOther::checkUnreachableCode()
tok = tok->link();
else if (Token::Match(tok, "break|continue ;"))
secondBreak = tok->tokAt(2);
else if (Token::Match(tok, "[;{}:] return|throw")) {
else if (Token::Match(tok, "[;{}:] return|throw") && tok->next()->isKeyword()) {
if (Token::simpleMatch(tok->astParent(), "?"))
continue;
tok = tok->next(); // tok should point to return or throw
@ -809,7 +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|return")) {
if (Token::Match(secondBreak, "continue|goto|throw|return") && secondBreak->isKeyword()) {
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

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