From 850ad0fed97839ff48a187a30dd1c06b5972d873 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 9 Mar 2022 18:21:47 +0100 Subject: [PATCH] #8451: Add error message for unused casts, log unhandled errors (#3883) * #8451: Add error message for unused casts, log unhandled errors * Fix TODO * Fix TODO --- lib/checkother.cpp | 12 +++++++++--- test/testincompletestatement.cpp | 5 ++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a8a2df548..d2a0bbde8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1833,7 +1833,7 @@ void CheckOther::checkIncompleteStatement() const Token *rtok = nextAfterAstRightmostLeaf(tok); if (!Token::simpleMatch(tok->astParent(), ";") && !Token::simpleMatch(rtok, ";") && - !Token::Match(tok->previous(), ";|}|{ %any% ;")) + !Token::Match(tok->previous(), ";|}|{ %any% ;") && !(mTokenizer->isCPP() && tok->isCast() && !tok->astParent())) continue; // Skip statement expressions if (Token::simpleMatch(rtok, "; } )")) @@ -1868,8 +1868,14 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type, msg = "Redundant code: Found a statement that begins with " + std::string(valueTok->isNumber() ? "numeric" : "string") + " constant."; else if (!tok) msg = "Redundant code: Found a statement that begins with " + type + " constant."; - else - return; // Strange! + else if (tok->isCast() && tok->tokType() == Token::Type::eExtendedOp) { + msg = "Found unused cast "; + msg += valueTok ? "of expression '" + valueTok->expressionString() + "'." : "expression."; + } + else { + reportError(tok, Severity::debug, "debug", "constStatementError not handled."); + return; + } reportError(tok, Severity::warning, "constStatement", msg, CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal); } diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index f50358888..8a816c891 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -170,7 +170,7 @@ private: " (void)c;\n" " }\n" "}"); - ASSERT_EQUALS("", errout.str()); + TODO_ASSERT_EQUALS("", "[test.cpp:9]: (debug) constStatementError not handled.\n", errout.str()); } void test_numeric() { @@ -375,6 +375,7 @@ private: "[test.cpp:5]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" "[test.cpp:6]: (warning, inconclusive) Found suspicious operator '!'\n" "[test.cpp:7]: (warning, inconclusive) Found suspicious operator '!'\n" + "[test.cpp:8]: (warning) Found unused cast of expression '!x'.\n" "[test.cpp:9]: (warning, inconclusive) Found suspicious operator '~'\n", errout.str()); check("void f1(int x) { x; }", true); @@ -383,6 +384,8 @@ private: check("void f() { if (Type t; g(t)) {} }"); // #9776 ASSERT_EQUALS("", errout.str()); + check("void f(int x) { static_cast(x); }"); + ASSERT_EQUALS("[test.cpp:1]: (warning) Found unused cast of expression 'x'.\n", errout.str()); } void vardecl() {