Skip to content

Commit 2ba733a

Browse files
committed
Fix 14136: False positive: constParameterReference when using void cast
1 parent 3e169d6 commit 2ba733a

5 files changed

Lines changed: 29 additions & 7 deletions

File tree

lib/astutils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ bool isStlStringType(const Token* tok)
429429
(Token::simpleMatch(tok, "std :: basic_string <") && !Token::simpleMatch(tok->linkAt(3), "> ::"));
430430
}
431431

432+
bool isVoidCast(const Token *tok)
433+
{
434+
return Token::simpleMatch(tok, "(") && tok->isCast() && tok->valueType() && tok->valueType()->type == ValueType::Type::VOID && tok->valueType()->pointer == 0;
435+
}
436+
432437
bool isTemporary(const Token* tok, const Library* library, bool unknown)
433438
{
434439
if (!tok)

lib/astutils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
188188
bool isVariableDecl(const Token* tok);
189189
bool isStlStringType(const Token* tok);
190190

191+
bool isVoidCast(const Token *tok);
192+
191193
bool isTemporary(const Token* tok, const Library* library, bool unknown = false);
192194

193195
const Token* previousBeforeAstLeftmostLeaf(const Token* tok);

lib/checkother.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,21 @@ static bool isVariableMutableInInitializer(const Token* start, const Token * end
16411641
return false;
16421642
}
16431643

1644+
static bool isCastToVoid(const Variable* var)
1645+
{
1646+
if(!var)
1647+
return false;
1648+
if(!var->scope())
1649+
return false;
1650+
for (const Token *tok = var->scope()->bodyStart; tok != var->scope()->bodyEnd; tok = tok->next()) {
1651+
if(tok->varId() != var->declarationId())
1652+
continue;
1653+
if(isVoidCast(tok->astParent()))
1654+
return true;
1655+
}
1656+
return false;
1657+
}
1658+
16441659
void CheckOther::checkConstVariable()
16451660
{
16461661
if ((!mSettings->severity.isEnabled(Severity::style) || mTokenizer->isC()) && !mSettings->isPremiumEnabled("constVariable"))
@@ -1689,6 +1704,8 @@ void CheckOther::checkConstVariable()
16891704
continue;
16901705
if (isStructuredBindingVariable(var)) // TODO: check all bound variables
16911706
continue;
1707+
if (isCastToVoid(var))
1708+
continue;
16921709
if (isVariableChanged(var, *mSettings))
16931710
continue;
16941711
const bool hasFunction = function != nullptr;

lib/checkuninitvar.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,11 +1150,6 @@ static bool astIsRhs(const Token *tok)
11501150
return tok && tok->astParent() && tok == tok->astParent()->astOperand2();
11511151
}
11521152

1153-
static bool isVoidCast(const Token *tok)
1154-
{
1155-
return Token::simpleMatch(tok, "(") && tok->isCast() && tok->valueType() && tok->valueType()->type == ValueType::Type::VOID && tok->valueType()->pointer == 0;
1156-
}
1157-
11581153
const Token* CheckUninitVar::isVariableUsage(const Token *vartok, const Library& library, bool pointer, Alloc alloc, int indirect)
11591154
{
11601155
const bool cpp = vartok->isCpp();

test/testother.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,10 @@ class TestOther : public TestFixture {
33003300
"};\n");
33013301
ASSERT_EQUALS("", errout_str());
33023302

3303+
// #14136
3304+
check("void f(int& x) { (void)x; }\n");
3305+
ASSERT_EQUALS("", errout_str());
3306+
33033307
check("void e();\n"
33043308
"void g(void);\n"
33053309
"void h(void);\n"
@@ -12160,8 +12164,7 @@ class TestOther : public TestFixture {
1216012164
" for (auto &j : g(std::move(l))) { (void)j; }\n"
1216112165
" }\n"
1216212166
"}\n");
12163-
ASSERT_EQUALS("[test.cpp:4:20]: (style) Variable 'j' can be declared as reference to const [constVariableReference]\n"
12164-
"[test.cpp:4:36]: (warning) Access of moved variable 'l'. [accessMoved]\n",
12167+
ASSERT_EQUALS("[test.cpp:4:36]: (warning) Access of moved variable 'l'. [accessMoved]\n",
1216512168
errout_str());
1216612169
}
1216712170

0 commit comments

Comments
 (0)