Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2323,18 +2323,12 @@ static bool isConstStatement(const Token *tok, const Library& library, bool plat

static bool isVoidStmt(const Token *tok)
{
if (Token::simpleMatch(tok, "( void"))
if (Token::simpleMatch(tok, "( void") && !(tok->astOperand1() && (tok->astOperand1()->isLiteral() || isNullOperand(tok->astOperand1()))))
return true;
if (isCPPCast(tok) && tok->astOperand1() && Token::Match(tok->astOperand1()->next(), "< void *| >"))
if (isCPPCast(tok) && tok->astOperand1() && Token::Match(tok->astOperand1()->next(), "< void *| >") &&
!(tok->astOperand2() && (tok->astOperand2()->isLiteral() || isNullOperand(tok->astOperand2()))))
return true;
const Token *tok2 = tok;
while (tok2->astOperand1())
tok2 = tok2->astOperand1();
if (Token::simpleMatch(tok2->previous(), ")") && Token::simpleMatch(tok2->linkAt(-1), "( void"))
return true;
if (Token::simpleMatch(tok2, "( void"))
return true;
return Token::Match(tok2->previous(), "delete|throw|return");
return false;
}

static bool isConstTop(const Token *tok)
Expand Down
4 changes: 2 additions & 2 deletions test/cli/proj-inline-suppress/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ void f()
{
#if DEF_1
// cppcheck-suppress id
(void)0;
;
#endif

// cppcheck-suppress id
(void)0;
;
}
18 changes: 17 additions & 1 deletion test/testincompletestatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,25 @@ class TestIncompleteStatement : public TestFixture {
}

void void0() { // #6327
check("void f() { (void*)0; }");
check("#define assert(x) ((void)0)\n"
"void f(int* p) {\n"
" assert(p);\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("void f() { (void*)0; }");
ASSERT_EQUALS("[test.cpp:1:12]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n", errout_str());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. the warning message is not technically correct. I would say that the cast comes before the numeric constant.
maybe we should try to reformulate this somehow.


check("void f() {\n" // #13148
" static_cast<void>(1);\n"
" static_cast<void>(nullptr);\n"
" (void)NULL;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:22]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
"[test.cpp:3:22]: (warning) Redundant code: Found a statement that begins with NULL constant. [constStatement]\n"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we would not say that nullptr ist a NULL constant. can we write nullptr instead or null?

"[test.cpp:4:5]: (warning) Redundant code: Found a statement that begins with NULL constant. [constStatement]\n",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test case it just looks like a redundant statement , I wonder if the severity should be "style" instead. Are there bugs discovered by this checker or just code cleanup?

We could change the severity in a separate PR.

errout_str());

check("#define X 0\n"
"void f() { X; }");
ASSERT_EQUALS("", errout_str());
Expand Down
7 changes: 5 additions & 2 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3914,7 +3914,9 @@ class TestOther : public TestFixture {
" (void)(true);\n"
" if (r) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1:13]: (style) Parameter 'r' can be declared as reference to const [constParameterReference]\n", errout_str());
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Redundant code: Found a statement that begins with bool constant. [constStatement]\n"
"[test.cpp:1:13]: (style) Parameter 'r' can be declared as reference to const [constParameterReference]\n",
errout_str());

check("struct S { void f(int&); };\n" // #12216
"void g(S& s, int& r, void (S::* p2m)(int&)) {\n"
Expand Down Expand Up @@ -7012,7 +7014,8 @@ class TestOther : public TestFixture {
" std::pair<int, int>(1, 2);\n"
" (void)0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n"
ASSERT_EQUALS("[test.cpp:5:5]: (warning) Redundant code: Found a statement that begins with numeric constant. [constStatement]\n"
"[test.cpp:2:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n"
"[test.cpp:3:10]: (style) Instance of 'std::string' object is destroyed immediately. [unusedScopedObject]\n"
"[test.cpp:4:10]: (style) Instance of 'std::pair' object is destroyed immediately. [unusedScopedObject]\n",
errout_str());
Expand Down
Loading