From 97eecb78a7bca951a13c103c8fbae6ac604894a4 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Wed, 7 Dec 2011 18:20:52 +0100 Subject: [PATCH] Fixed #3381 (false positive: (style) Statements following return, break, continue, goto or throw will never be executed) --- lib/checkother.cpp | 9 ++++++--- test/testother.cpp | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 28bc0ee9e..ab12a39c1 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1451,12 +1451,15 @@ void CheckOther::checkUnreachableCode() for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { 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); - 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()) if (tok2->str() == ";") { - secondBreak = tok2->tokAt(1); + secondBreak = tok2->next(); break; } } else if (Token::Match(tok, "goto %any% ;")) diff --git a/test/testother.cpp b/test/testother.cpp index 520bb4fcb..765478419 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -80,7 +80,7 @@ private: TEST_CASE(switchRedundantAssignmentTest); TEST_CASE(switchFallThroughCase); - TEST_CASE(duplicateBreak); + TEST_CASE(unreachableCode); TEST_CASE(coutCerrMisusage); @@ -1764,7 +1764,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void duplicateBreak() { + void unreachableCode() { check("void foo(int a) {\n" " while(1) {\n" " if (a++ >= 100) {\n" @@ -1905,6 +1905,18 @@ private: " throw 0;\n" "}"); 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()); }