Skip to content

Commit d9049eb

Browse files
authored
Fix 14136: False positive: constParameterReference when using void cast (#7829)
1 parent 3f93d61 commit d9049eb

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

lib/astutils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,12 @@ 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() &&
435+
tok->valueType()->type == ValueType::Type::VOID && tok->valueType()->pointer == 0;
436+
}
437+
432438
bool isTemporary(const Token* tok, const Library* library, bool unknown)
433439
{
434440
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 & 3 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"
@@ -12173,9 +12177,7 @@ class TestOther : public TestFixture {
1217312177
" for (auto &j : g(std::move(l))) { (void)j; }\n"
1217412178
" }\n"
1217512179
"}\n");
12176-
ASSERT_EQUALS("[test.cpp:4:20]: (style) Variable 'j' can be declared as reference to const [constVariableReference]\n"
12177-
"[test.cpp:4:36]: (warning) Access of moved variable 'l'. [accessMoved]\n",
12178-
errout_str());
12180+
ASSERT_EQUALS("[test.cpp:4:36]: (warning) Access of moved variable 'l'. [accessMoved]\n", errout_str());
1217912181
}
1218012182

1218112183
void moveCallback()

0 commit comments

Comments
 (0)