Skip to content

Commit c7ef602

Browse files
committed
Fixed #9759 (False positive: constParameter on parameter used by non-const call via pointer to member function)
1 parent cdc34fe commit c7ef602

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

lib/checkother.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,20 @@ void CheckOther::checkConstVariable()
14661466
if (changeStructData)
14671467
continue;
14681468
}
1469+
// Calling non-const method using non-const reference
1470+
if (var->isReference()) {
1471+
bool callNonConstMethod = false;
1472+
for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) {
1473+
if (tok->variable() == var && Token::Match(tok, "%var% . * ( & %name% ::")) {
1474+
const Token *ftok = tok->linkAt(3)->previous();
1475+
if (!ftok->function() || !ftok->function()->isConst())
1476+
callNonConstMethod = true;
1477+
break;
1478+
}
1479+
}
1480+
if (callNonConstMethod)
1481+
continue;
1482+
}
14691483

14701484
constVariableError(var, function);
14711485
}

test/testother.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,6 +2615,10 @@ class TestOther : public TestFixture {
26152615
" panels.erase(it);\n"
26162616
"}");
26172617
ASSERT_EQUALS("", errout.str());
2618+
2619+
check("struct S { void f(); int i; };\n"
2620+
"void call_f(S& s) { (s.*(&S::f))(); }\n");
2621+
ASSERT_EQUALS("", errout.str());
26182622
}
26192623

26202624
void constParameterCallback() {

0 commit comments

Comments
 (0)