diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index cae26e77f..02f2903a4 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1663,7 +1663,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok) // function call.. 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)) { diff --git a/test/testclass.cpp b/test/testclass.cpp index 81ef21d5c..310d414c1 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -185,6 +185,7 @@ private: TEST_CASE(const52); // ticket #3049 TEST_CASE(const53); // ticket #3052 TEST_CASE(const54); + TEST_CASE(const55); // ticket #3149 TEST_CASE(assigningPointerToPointerIsNotAConstOperation); TEST_CASE(assigningArrayElementIsNotAConstOperation); TEST_CASE(constoperator1); // operator< can often be const @@ -5756,6 +5757,17 @@ private: 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() { checkConst("struct s\n"