Skip to content

Commit f8b92a1

Browse files
committed
Refs #14294: Fix FN constParameterPointer for cast to integer
1 parent 602da94 commit f8b92a1

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/checkother.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,11 @@ namespace {
18311831
};
18321832
}
18331833

1834+
static bool isCastToInteger(const Token* tok)
1835+
{
1836+
return tok && tok->isCast() && tok->valueType() && tok->valueType()->isIntegral() && tok->valueType()->pointer == 0;
1837+
}
1838+
18341839
void CheckOther::checkConstPointer()
18351840
{
18361841
if (!mSettings->severity.isEnabled(Severity::style) &&
@@ -1883,6 +1888,8 @@ void CheckOther::checkConstPointer()
18831888
deref = MEMBER;
18841889
else if (astIsRangeBasedForDecl(tok))
18851890
continue;
1891+
else if (isCastToInteger(parent))
1892+
continue;
18861893
if (deref != NONE) {
18871894
const Token* gparent = parent->astParent();
18881895
while (Token::simpleMatch(gparent, "[") && parent != gparent->astOperand2() && parent->str() == gparent->str())

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7031,7 +7031,7 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
70317031
setValueType(parent, vt);
70327032
return;
70337033
}
7034-
if (Token::Match(parent->previous(), "%name% (") && parent->astOperand1() == tok && valuetype.pointer > 0U) {
7034+
if (Token::Match(parent->tokAt(-1), "%name% (") && !parent->tokAt(-1)->isKeyword() && parent->astOperand1() == tok && valuetype.pointer > 0U) {
70357035
ValueType vt(valuetype);
70367036
vt.pointer -= 1U;
70377037
setValueType(parent, vt);

test/testother.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4598,6 +4598,19 @@ class TestOther : public TestFixture {
45984598
"}\n");
45994599
ASSERT_EQUALS("[test.cpp:4:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
46004600
errout_str());
4601+
4602+
check("uintptr_t f(int* p) {\n"
4603+
" return (uintptr_t)p;\n"
4604+
"}\n"
4605+
"uintptr_t g(int* p) {\n"
4606+
" return static_cast<uintptr_t>(p);\n"
4607+
"}\n"
4608+
"U h(int* p) {\n"
4609+
" return (U)p;\n"
4610+
"}\n");
4611+
ASSERT_EQUALS("[test.cpp:1:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
4612+
"[test.cpp:4:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n",
4613+
errout_str());
46014614
}
46024615

46034616
void constArray() {

0 commit comments

Comments
 (0)