From b4b97e57067fedc805f011a5b5e3edc6abe547b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 24 Jan 2010 13:33:30 +0100 Subject: [PATCH] const: fixed false positive when using increment/decrement --- lib/checkclass.cpp | 7 +++++++ test/testclass.cpp | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) 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)