fix #3149 (false negative: Technically the member function 'A::f' can be const.)

This commit is contained in:
Robert Reif 2011-09-29 20:16:52 -04:00
parent ac942c6684
commit 556d523e4f
2 changed files with 13 additions and 1 deletions

View File

@ -1663,7 +1663,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok)
// function call.. // function call..
else if (Token::Match(tok1, "%var% (") && else if (Token::Match(tok1, "%var% (") &&
!(Token::Match(tok1, "return|c_str|if|string") || tok1->isStandardType())) !(Token::Match(tok1, "return|c_str|if|string|switch|while") || tok1->isStandardType()))
{ {
if (!isConstMemberFunc(scope, tok1)) if (!isConstMemberFunc(scope, tok1))
{ {

View File

@ -185,6 +185,7 @@ private:
TEST_CASE(const52); // ticket #3049 TEST_CASE(const52); // ticket #3049
TEST_CASE(const53); // ticket #3052 TEST_CASE(const53); // ticket #3052
TEST_CASE(const54); TEST_CASE(const54);
TEST_CASE(const55); // ticket #3149
TEST_CASE(assigningPointerToPointerIsNotAConstOperation); TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
TEST_CASE(assigningArrayElementIsNotAConstOperation); TEST_CASE(assigningArrayElementIsNotAConstOperation);
TEST_CASE(constoperator1); // operator< can often be const TEST_CASE(constoperator1); // operator< can often be const
@ -5756,6 +5757,17 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void const55() // ticket #3149
{
checkConst("class MyObject {\n"
"public:\n"
" void foo(int x) {\n"
" switch (x) { }\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:3]: (information) Technically the member function 'MyObject::foo' can be const.\n", errout.str());
}
void assigningPointerToPointerIsNotAConstOperation() void assigningPointerToPointerIsNotAConstOperation()
{ {
checkConst("struct s\n" checkConst("struct s\n"