Fixed #9759 (False positive: constParameter on parameter used by non-const call via pointer to member function)

This commit is contained in:
Daniel Marjamäki 2021-11-21 20:03:10 +01:00
parent cdc34fe92f
commit c7ef602cd6
2 changed files with 18 additions and 0 deletions

View File

@ -1466,6 +1466,20 @@ void CheckOther::checkConstVariable()
if (changeStructData)
continue;
}
// Calling non-const method using non-const reference
if (var->isReference()) {
bool callNonConstMethod = false;
for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) {
if (tok->variable() == var && Token::Match(tok, "%var% . * ( & %name% ::")) {
const Token *ftok = tok->linkAt(3)->previous();
if (!ftok->function() || !ftok->function()->isConst())
callNonConstMethod = true;
break;
}
}
if (callNonConstMethod)
continue;
}
constVariableError(var, function);
}

View File

@ -2615,6 +2615,10 @@ private:
" panels.erase(it);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct S { void f(); int i; };\n"
"void call_f(S& s) { (s.*(&S::f))(); }\n");
ASSERT_EQUALS("", errout.str());
}
void constParameterCallback() {