diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 3aff88e11..2c63e8db6 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1452,7 +1452,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func) return(false); } else if (Token::Match(tok1, "%var% . %var% (")) { if (!isMemberVar(scope, tok1)) - tok1 = tok1->tokAt(2); + tok1 = tok1->next(); else if (tok1->varId() && (Token::Match(tok1->tokAt(2), "size|empty|cend|crend|cbegin|crbegin|max_size|length|count|capacity|get_allocator|c_str|str ( )") || Token::Match(tok1->tokAt(2), "rfind|copy"))) { const Variable *var = symbolDatabase->getVariableFromVarId(tok1->varId()); diff --git a/test/testclass.cpp b/test/testclass.cpp index f14323189..e5395453d 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -135,6 +135,7 @@ private: TEST_CASE(const57); // ticket #2669 TEST_CASE(const58); // ticket #2698 TEST_CASE(const_handleDefaultParameters); + TEST_CASE(const_passThisToMemberOfOtherClass); TEST_CASE(assigningPointerToPointerIsNotAConstOperation); TEST_CASE(assigningArrayElementIsNotAConstOperation); TEST_CASE(constoperator1); // operator< can often be const @@ -4353,6 +4354,24 @@ private: "[test.cpp:14]: (style, inconclusive) Technically the member function 'Foo::bar4' can be const.\n", errout.str()); } + void const_passThisToMemberOfOtherClass() { + checkConst("struct Foo {\n" + " void foo() {\n" + " Bar b;\n" + " b.takeFoo(this);\n" + " }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + checkConst("struct Foo {\n" + " void foo() {\n" + " Foo f;\n" + " f.foo();\n" + " }\n" + "};"); + ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Technically the member function 'Foo::foo' can be const.\n", errout.str()); + } + void assigningPointerToPointerIsNotAConstOperation() { checkConst("struct s\n" "{\n"