Fixed false positive "Function can be const" when 'this' is passed to a Memberfunction

This commit is contained in:
PKEuS 2012-05-17 01:49:52 -07:00
parent 4bb2a1b27b
commit 3f5712bfb8
2 changed files with 20 additions and 1 deletions

View File

@ -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());

View File

@ -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"