diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 784ccfc8d..6f5219073 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1470,6 +1470,13 @@ void CheckClass::checkConst() break; } + // increment/decrement (member variable?).. + else if (Token::Match(tok3, "++|--")) + { + isconst = false; + break; + } + // function call.. else if (tok3->str() != "return" && Token::Match(tok3, "%var% (")) { diff --git a/test/testclass.cpp b/test/testclass.cpp index 425f96737..dceb343bd 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -81,7 +81,8 @@ private: // can member function be made const TEST_CASE(const1); - TEST_CASE(const2); + TEST_CASE(constoperator); // operator< can often be const + TEST_CASE(constincdec); // increment/decrement => non-const } // Check the operator Equal @@ -1558,7 +1559,7 @@ private: } // operator< can often be const - void const2() + void constoperator() { checkConst("struct Fred {\n" " int a;\n" @@ -1566,6 +1567,16 @@ private: "};\n"); ASSERT_EQUALS("[test.cpp:3]: (style) The function 'Fred::operator<' can be const\n", errout.str()); } + + // increment/decrement => not const + void constincdec() + { + checkConst("class Fred {\n" + " int a;\n" + " void nextA() { return ++a; }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestClass)