Fixed #3381 (false positive: (style) Statements following return, break, continue, goto or throw will never be executed)

This commit is contained in:
PKEuS 2011-12-07 18:20:52 +01:00 committed by Daniel Marjamäki
parent ab348387b5
commit 97eecb78a7
2 changed files with 20 additions and 5 deletions

View File

@ -1451,12 +1451,15 @@ void CheckOther::checkUnreachableCode()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const Token* secondBreak = 0; const Token* secondBreak = 0;
if (Token::Match(tok, "break|continue ;")) if (tok->str() == "(")
tok = tok->link();
else if (Token::Match(tok, "break|continue ;"))
secondBreak = tok->tokAt(2); secondBreak = tok->tokAt(2);
else if (tok->str() == "return" || tok->str() == "throw") { else if (Token::Match(tok, "[;{}:] return|throw")) {
tok = tok->next(); // tok should point to return or throw
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
if (tok2->str() == ";") { if (tok2->str() == ";") {
secondBreak = tok2->tokAt(1); secondBreak = tok2->next();
break; break;
} }
} else if (Token::Match(tok, "goto %any% ;")) } else if (Token::Match(tok, "goto %any% ;"))

View File

@ -80,7 +80,7 @@ private:
TEST_CASE(switchRedundantAssignmentTest); TEST_CASE(switchRedundantAssignmentTest);
TEST_CASE(switchFallThroughCase); TEST_CASE(switchFallThroughCase);
TEST_CASE(duplicateBreak); TEST_CASE(unreachableCode);
TEST_CASE(coutCerrMisusage); TEST_CASE(coutCerrMisusage);
@ -1764,7 +1764,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void duplicateBreak() { void unreachableCode() {
check("void foo(int a) {\n" check("void foo(int a) {\n"
" while(1) {\n" " while(1) {\n"
" if (a++ >= 100) {\n" " if (a++ >= 100) {\n"
@ -1905,6 +1905,18 @@ private:
" throw 0;\n" " throw 0;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void foo() {\n"
" wxCHECK2(state < 3 && state >= 0, return);\n"
" _checkboxState = state;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct A {\n"
" virtual void foo (P & Val) throw ();\n"
" virtual void foo1 (P & Val) throw ();\n"
"}");
ASSERT_EQUALS("", errout.str());
} }