diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ee84dec44..9219eac8a 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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); } diff --git a/test/testother.cpp b/test/testother.cpp index 1d8e4e1b3..5fcdd46c7 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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() {