From cf8a423636ec8fcb8518c05a9b55efb2b0a64637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 5 Apr 2021 04:53:07 +0200 Subject: [PATCH] Unreachable code; better handling of throw in C code --- lib/checkother.cpp | 4 ++-- test/testother.cpp | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index db3bb41ef..dc40d73a4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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 diff --git a/test/testother.cpp b/test/testother.cpp index 898297e77..a37676433 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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"