#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
This commit is contained in:
chrchr-github 2022-03-09 18:21:47 +01:00 committed by GitHub
parent c9f47dec8b
commit 850ad0fed9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -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);
}

View File

@ -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<unsigned>(x); }");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found unused cast of expression 'x'.\n", errout.str());
}
void vardecl() {