From d5e10c18d323218991b20e61f4926117b83a3b68 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Mon, 22 Dec 2014 11:15:22 +0100 Subject: [PATCH] checkUnreachableCode(): fix FP for statements that just hide compiler warnings about unused function arguments Seen throughout the rockbox codebase. --- lib/checkother.cpp | 18 +++++++++++++++++- test/testother.cpp | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index dc97baa05..9295a0995 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1189,7 +1189,23 @@ void CheckOther::checkUnreachableCode() } } } - if (!labelInFollowingLoop) + + // hide FP for statements that just hide compiler warnings about unused function arguments + bool silencedCompilerWarningOnly = false; + const Token *silencedWarning = secondBreak; + for (;;) { + if (Token::Match(silencedWarning, "( void ) %var% ;")) { + silencedWarning = silencedWarning->tokAt(5); + continue; + } else if (silencedWarning && silencedWarning == scope->classEnd) + silencedCompilerWarningOnly = true; + + break; + } + if (silencedWarning) + secondBreak = silencedWarning; + + if (!labelInFollowingLoop && !silencedCompilerWarningOnly) unreachableCodeError(secondBreak, inconclusive); tok = Token::findmatch(secondBreak, "[}:]"); } else diff --git a/test/testother.cpp b/test/testother.cpp index e49c6da42..7c2605442 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2965,6 +2965,27 @@ private: "}", 0, false, false, false, false); ASSERT_EQUALS("[test.cpp:3]: (style) Statements following return, break, continue, goto or throw will never be executed.\n", errout.str()); + check("int foo(int unused) {\n" + " return 0;\n" + " (void)unused;\n" + "}", 0, false, false, false, false); + ASSERT_EQUALS("", errout.str()); + + check("int foo(int unused1, int unused2) {\n" + " return 0;\n" + " (void)unused1;\n" + " (void)unused2;\n" + "}", 0, false, false, false, false); + ASSERT_EQUALS("", errout.str()); + + check("int foo(int unused1, int unused2) {\n" + " return 0;\n" + " (void)unused1;\n" + " (void)unused2;\n" + " foo();\n" + "}", 0, false, false, false, false); + ASSERT_EQUALS("[test.cpp:5]: (style) Statements following return, break, continue, goto or throw will never be executed.\n", errout.str()); + check("int foo() {\n" " if(bar)\n" " return 0;\n"