diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index d917ed8ce..0672ad15b 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1546,7 +1546,17 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok) // increment/decrement (member variable?).. else if (Token::Match(tok1, "++|--")) { - isconst = false; + if (Token::Match(tok1->previous(), "%var%") && + tok1->previous()->str() != "return") + { + if (isMemberVar(scope, tok1->previous())) + isconst = false; + } + else if (Token::Match(tok1->next(), "%var%")) + { + if (isMemberVar(scope, tok1->next())) + isconst = false; + } break; } diff --git a/test/testclass.cpp b/test/testclass.cpp index 2524e6b9f..f27d2a7a0 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -166,6 +166,7 @@ private: TEST_CASE(const43); // ticket #2377 TEST_CASE(const44); // ticket #2595 TEST_CASE(const45); // ticket #2664 + TEST_CASE(const46); // ticket #2636 TEST_CASE(assigningPointerToPointerIsNotAConstOperation); TEST_CASE(assigningArrayElementIsNotAConstOperation); TEST_CASE(constoperator1); // operator< can often be const @@ -5210,6 +5211,23 @@ private: ASSERT_EQUALS("[test.cpp:8]: (information) Technically the member function 'tools::WorkspaceControl::toGrid' can be const.\n", errout.str()); } + void const46() // ticket 2663 + { + checkConst("class Altren {\n" + "public:\n" + " int fun1() {\n" + " int a;\n" + " a++;\n" + " }\n" + " int fun2() {\n" + " b++;\n" + " }\n" + "}\n"); + + ASSERT_EQUALS("[test.cpp:3]: (information) Technically the member function 'Altren::fun1' can be const.\n" + "[test.cpp:7]: (information) Technically the member function 'Altren::fun2' can be const.\n", errout.str()); + } + void assigningPointerToPointerIsNotAConstOperation() { checkConst("struct s\n"