Skip to content

Commit 174a569

Browse files
Fix #13148 FN: constStatement (static_cast<void>(integer|nullptr|NULL)) (#8694)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 3d88880 commit 174a569

4 files changed

Lines changed: 48 additions & 41 deletions

File tree

lib/checkother.cpp

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,18 +2323,12 @@ static bool isConstStatement(const Token *tok, const Library& library, bool plat
23232323

23242324
static bool isVoidStmt(const Token *tok)
23252325
{
2326-
if (Token::simpleMatch(tok, "( void"))
2326+
if (Token::simpleMatch(tok, "( void") && !(tok->astOperand1() && (tok->astOperand1()->isLiteral() || isNullOperand(tok->astOperand1()))))
23272327
return true;
2328-
if (isCPPCast(tok) && tok->astOperand1() && Token::Match(tok->astOperand1()->next(), "< void *| >"))
2328+
if (isCPPCast(tok) && tok->astOperand1() && Token::Match(tok->astOperand1()->next(), "< void *| >") &&
2329+
!(tok->astOperand2() && (tok->astOperand2()->isLiteral() || isNullOperand(tok->astOperand2()))))
23292330
return true;
2330-
const Token *tok2 = tok;
2331-
while (tok2->astOperand1())
2332-
tok2 = tok2->astOperand1();
2333-
if (Token::simpleMatch(tok2->previous(), ")") && Token::simpleMatch(tok2->linkAt(-1), "( void"))
2334-
return true;
2335-
if (Token::simpleMatch(tok2, "( void"))
2336-
return true;
2337-
return Token::Match(tok2->previous(), "delete|throw|return");
2331+
return false;
23382332
}
23392333

23402334
static bool isConstTop(const Token *tok)
@@ -2425,37 +2419,31 @@ void CheckOtherImpl::checkIncompleteStatement()
24252419

24262420
void CheckOtherImpl::constStatementError(const Token *tok, const std::string &type, bool inconclusive)
24272421
{
2428-
const Token *valueTok = tok;
2429-
while (valueTok && valueTok->isCast())
2430-
valueTok = valueTok->astOperand2() ? valueTok->astOperand2() : valueTok->astOperand1();
2431-
24322422
std::string msg;
24332423
if (Token::simpleMatch(tok, "=="))
24342424
msg = "Found suspicious equality comparison. Did you intend to assign a value instead?";
24352425
else if (Token::Match(tok, ",|!|~|%cop%"))
24362426
msg = "Found suspicious operator '" + tok->str() + "', result is not used.";
24372427
else if (Token::Match(tok, "%var%"))
24382428
msg = "Unused variable value '" + tok->str() + "'";
2439-
else if (isConstant(valueTok)) {
2429+
else if (isConstant(tok)) {
24402430
std::string typeStr("string");
2441-
if (valueTok->isNumber())
2431+
if (tok->isNumber())
24422432
typeStr = "numeric";
2443-
else if (valueTok->isBoolean())
2433+
else if (tok->isBoolean())
24442434
typeStr = "bool";
2445-
else if (valueTok->tokType() == Token::eChar)
2435+
else if (tok->tokType() == Token::eChar)
24462436
typeStr = "character";
2447-
else if (isNullOperand(valueTok))
2448-
typeStr = "NULL";
2449-
else if (valueTok->isEnumerator())
2437+
else if (isNullOperand(tok))
2438+
typeStr = "null";
2439+
else if (tok->isEnumerator())
24502440
typeStr = "enumerator";
24512441
msg = "Redundant code: Found a statement that begins with " + typeStr + " constant.";
24522442
}
24532443
else if (!tok)
24542444
msg = "Redundant code: Found a statement that begins with " + type + " constant.";
2455-
else if (tok->isCast() && tok->tokType() == Token::Type::eExtendedOp) {
2456-
msg = "Redundant code: Found unused cast ";
2457-
msg += valueTok ? "of expression '" + valueTok->expressionString() + "'." : "expression.";
2458-
}
2445+
else if (tok->isCast() && tok->tokType() == Token::Type::eExtendedOp)
2446+
msg = "Redundant code: Found unused cast in expression '" + tok->expressionString() + "'.";
24592447
else if (tok->str() == "?" && tok->tokType() == Token::Type::eExtendedOp)
24602448
msg = "Redundant code: Found unused result of ternary operator.";
24612449
else if (tok->str() == "." && tok->tokType() == Token::Type::eOther)

test/cli/proj-inline-suppress/cfg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ void f()
22
{
33
#if DEF_1
44
// cppcheck-suppress id
5-
(void)0;
5+
;
66
#endif
77

88
// cppcheck-suppress id
9-
(void)0;
9+
;
1010
}

test/testincompletestatement.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,25 @@ class TestIncompleteStatement : public TestFixture {
180180
}
181181

182182
void void0() { // #6327
183-
check("void f() { (void*)0; }");
183+
check("#define assert(x) ((void)0)\n"
184+
"void f(int* p) {\n"
185+
" assert(p);\n"
186+
"}\n");
184187
ASSERT_EQUALS("", errout_str());
185188

189+
check("void f() { (void*)0; }");
190+
ASSERT_EQUALS("[test.cpp:1:12]: (warning) Redundant code: Found unused cast in expression '(void*)0'. [constStatement]\n", errout_str());
191+
192+
check("void f() {\n" // #13148
193+
" static_cast<void>(1);\n"
194+
" static_cast<void>(nullptr);\n"
195+
" (void)NULL;\n"
196+
"}\n");
197+
ASSERT_EQUALS("[test.cpp:2:22]: (warning) Redundant code: Found unused cast in expression 'static_cast<void>(1)'. [constStatement]\n"
198+
"[test.cpp:3:22]: (warning) Redundant code: Found unused cast in expression 'static_cast<void>(nullptr)'. [constStatement]\n"
199+
"[test.cpp:4:5]: (warning) Redundant code: Found unused cast in expression '(void)NULL'. [constStatement]\n",
200+
errout_str());
201+
186202
check("#define X 0\n"
187203
"void f() { X; }");
188204
ASSERT_EQUALS("", errout_str());
@@ -432,11 +448,11 @@ class TestIncompleteStatement : public TestFixture {
432448
"}\n", dinit(CheckOptions, $.inconclusive = true));
433449
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
434450
"[test.cpp:3:6]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
435-
"[test.cpp:4:5]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
436-
"[test.cpp:5:6]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
451+
"[test.cpp:4:5]: (warning) Redundant code: Found unused cast in expression '(char)1'. [constStatement]\n"
452+
"[test.cpp:5:6]: (warning) Redundant code: Found unused cast in expression '(char)1'. [constStatement]\n"
437453
"[test.cpp:6:5]: (warning, inconclusive) Found suspicious operator '!', result is not used. [constStatement]\n"
438454
"[test.cpp:7:6]: (warning, inconclusive) Found suspicious operator '!', result is not used. [constStatement]\n"
439-
"[test.cpp:8:5]: (warning) Redundant code: Found unused cast of expression '!x'. [constStatement]\n"
455+
"[test.cpp:8:5]: (warning) Redundant code: Found unused cast in expression '(unsigned int)!x'. [constStatement]\n"
440456
"[test.cpp:9:5]: (warning, inconclusive) Found suspicious operator '~', result is not used. [constStatement]\n",
441457
errout_str());
442458

@@ -447,7 +463,7 @@ class TestIncompleteStatement : public TestFixture {
447463
ASSERT_EQUALS("", errout_str());
448464

449465
check("void f(int x) { static_cast<unsigned>(x); }");
450-
ASSERT_EQUALS("[test.cpp:1:38]: (warning) Redundant code: Found unused cast of expression 'x'. [constStatement]\n", errout_str());
466+
ASSERT_EQUALS("[test.cpp:1:38]: (warning) Redundant code: Found unused cast in expression 'static_cast<unsigned int>(x)'. [constStatement]\n", errout_str());
451467

452468
check("void f(int x, int* p) {\n"
453469
" static_cast<void>(x);\n"
@@ -465,9 +481,9 @@ class TestIncompleteStatement : public TestFixture {
465481
" static_cast<float>((char)i);\n"
466482
" (char)static_cast<float>(i);\n"
467483
"}\n");
468-
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n"
469-
"[test.cpp:3:23]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n"
470-
"[test.cpp:4:5]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n",
484+
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found unused cast in expression '(float)(char)i'. [constStatement]\n"
485+
"[test.cpp:3:23]: (warning) Redundant code: Found unused cast in expression 'static_cast<float>((char)i)'. [constStatement]\n"
486+
"[test.cpp:4:5]: (warning) Redundant code: Found unused cast in expression '(char)static_cast<float>(i)'. [constStatement]\n",
471487
errout_str());
472488

473489
check("namespace M {\n"
@@ -476,7 +492,7 @@ class TestIncompleteStatement : public TestFixture {
476492
"void f(int i) {\n"
477493
" (M::N::T)i;\n"
478494
"}\n");
479-
ASSERT_EQUALS("[test.cpp:5:5]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n", errout_str());
495+
ASSERT_EQUALS("[test.cpp:5:5]: (warning) Redundant code: Found unused cast in expression '(char)i'. [constStatement]\n", errout_str());
480496

481497
check("void f(int (g)(int a, int b)) {\n" // #10873
482498
" int p = 0, q = 1;\n"
@@ -528,7 +544,7 @@ class TestIncompleteStatement : public TestFixture {
528544
" for (L\"y\"; ;) {}\n"
529545
"}\n");
530546
ASSERT_EQUALS("[test.cpp:2:10]: (warning) Unused variable value 'i' [constStatement]\n"
531-
"[test.cpp:3:10]: (warning) Redundant code: Found unused cast of expression 'i'. [constStatement]\n"
547+
"[test.cpp:3:10]: (warning) Redundant code: Found unused cast in expression '(long)i'. [constStatement]\n"
532548
"[test.cpp:4:10]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
533549
"[test.cpp:5:10]: (warning) Redundant code: Found a statement that begins with bool constant. [constStatement]\n"
534550
"[test.cpp:6:10]: (warning) Redundant code: Found a statement that begins with character constant. [constStatement]\n"
@@ -696,8 +712,8 @@ class TestIncompleteStatement : public TestFixture {
696712
" NULL;\n"
697713
" nullptr;\n"
698714
"}\n");
699-
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found a statement that begins with NULL constant. [constStatement]\n"
700-
"[test.cpp:3:5]: (warning) Redundant code: Found a statement that begins with NULL constant. [constStatement]\n",
715+
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found a statement that begins with null constant. [constStatement]\n"
716+
"[test.cpp:3:5]: (warning) Redundant code: Found a statement that begins with null constant. [constStatement]\n",
701717
errout_str());
702718

703719
check("struct S { int i; };\n" // #6504

test/testother.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3914,7 +3914,9 @@ class TestOther : public TestFixture {
39143914
" (void)(true);\n"
39153915
" if (r) {}\n"
39163916
"}\n");
3917-
ASSERT_EQUALS("[test.cpp:1:13]: (style) Parameter 'r' can be declared as reference to const [constParameterReference]\n", errout_str());
3917+
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found unused cast in expression '(void)(true)'. [constStatement]\n"
3918+
"[test.cpp:1:13]: (style) Parameter 'r' can be declared as reference to const [constParameterReference]\n",
3919+
errout_str());
39183920

39193921
check("struct S { void f(int&); };\n" // #12216
39203922
"void g(S& s, int& r, void (S::* p2m)(int&)) {\n"
@@ -7012,7 +7014,8 @@ class TestOther : public TestFixture {
70127014
" std::pair<int, int>(1, 2);\n"
70137015
" (void)0;\n"
70147016
"}\n");
7015-
ASSERT_EQUALS("[test.cpp:2:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n"
7017+
ASSERT_EQUALS("[test.cpp:5:5]: (warning) Redundant code: Found unused cast in expression '(void)0'. [constStatement]\n"
7018+
"[test.cpp:2:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n"
70167019
"[test.cpp:3:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n"
70177020
"[test.cpp:4:10]: (style) Instance of 'std::pair' object is destroyed immediately. [unusedScopedObject]\n",
70187021
errout_str());

0 commit comments

Comments
 (0)