Skip to content
Merged
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
6 changes: 6 additions & 0 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ bool isStlStringType(const Token* tok)
(Token::simpleMatch(tok, "std :: basic_string <") && !Token::simpleMatch(tok->linkAt(3), "> ::"));
}

bool isVoidCast(const Token* tok)
{
return Token::simpleMatch(tok, "(") && tok->isCast() && tok->valueType() &&
tok->valueType()->type == ValueType::Type::VOID && tok->valueType()->pointer == 0;
}

bool isTemporary(const Token* tok, const Library* library, bool unknown)
{
if (!tok)
Expand Down
2 changes: 2 additions & 0 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
bool isVariableDecl(const Token* tok);
bool isStlStringType(const Token* tok);

bool isVoidCast(const Token* tok);

bool isTemporary(const Token* tok, const Library* library, bool unknown = false);

const Token* previousBeforeAstLeftmostLeaf(const Token* tok);
Expand Down
17 changes: 17 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,21 @@ static bool isVariableMutableInInitializer(const Token* start, const Token * end
return false;
}

static bool isCastToVoid(const Variable* var)
{
if (!var)
return false;
if (!var->scope())
return false;
for (const Token* tok = var->scope()->bodyStart; tok != var->scope()->bodyEnd; tok = tok->next()) {
if (tok->varId() != var->declarationId())
continue;
if (isVoidCast(tok->astParent()))
return true;
}
return false;
}

void CheckOther::checkConstVariable()
{
if ((!mSettings->severity.isEnabled(Severity::style) || mTokenizer->isC()) && !mSettings->isPremiumEnabled("constVariable"))
Expand Down Expand Up @@ -1689,6 +1704,8 @@ void CheckOther::checkConstVariable()
continue;
if (isStructuredBindingVariable(var)) // TODO: check all bound variables
continue;
if (isCastToVoid(var))
continue;
if (isVariableChanged(var, *mSettings))
continue;
const bool hasFunction = function != nullptr;
Expand Down
5 changes: 0 additions & 5 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,11 +1150,6 @@ static bool astIsRhs(const Token *tok)
return tok && tok->astParent() && tok == tok->astParent()->astOperand2();
}

static bool isVoidCast(const Token *tok)
{
return Token::simpleMatch(tok, "(") && tok->isCast() && tok->valueType() && tok->valueType()->type == ValueType::Type::VOID && tok->valueType()->pointer == 0;
}

const Token* CheckUninitVar::isVariableUsage(const Token *vartok, const Library& library, bool pointer, Alloc alloc, int indirect)
{
const bool cpp = vartok->isCpp();
Expand Down
8 changes: 5 additions & 3 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,10 @@ class TestOther : public TestFixture {
"};\n");
ASSERT_EQUALS("", errout_str());

// #14136
check("void f(int& x) { (void)x; }\n");
ASSERT_EQUALS("", errout_str());

check("void e();\n"
"void g(void);\n"
"void h(void);\n"
Expand Down Expand Up @@ -12160,9 +12164,7 @@ class TestOther : public TestFixture {
" for (auto &j : g(std::move(l))) { (void)j; }\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:20]: (style) Variable 'j' can be declared as reference to const [constVariableReference]\n"
"[test.cpp:4:36]: (warning) Access of moved variable 'l'. [accessMoved]\n",
errout_str());
ASSERT_EQUALS("[test.cpp:4:36]: (warning) Access of moved variable 'l'. [accessMoved]\n", errout_str());
}

void moveCallback()
Expand Down