From 907ed0ac6fb2a3977cf38dfd3601f13f78477514 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Tue, 12 Oct 2010 07:57:09 +0200 Subject: [PATCH] Fixed #2085 (False negative: function can be const when member variable is compared) --- lib/checkclass.cpp | 3 ++- test/testclass.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index f6ac59ccc..44b4afc3f 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2200,7 +2200,8 @@ bool CheckClass::checkConstFunc(const SpaceInfo *info, const Token *tok) } // function call.. - else if ((Token::Match(tok1, "%var% (") && !Token::Match(tok1, "return|c_str|if|string")) || + else if ((Token::Match(tok1, "%var% (") && + !(Token::Match(tok1, "return|c_str|if|string") || tok1->isStandardType())) || Token::Match(tok1, "%var% < %any% > (")) { isconst = false; diff --git a/test/testclass.cpp b/test/testclass.cpp index a5678866a..34e9dbae2 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -144,6 +144,7 @@ private: TEST_CASE(const34); // ticket #1964 TEST_CASE(const35); // ticket #2001 TEST_CASE(const36); // ticket #2003 + TEST_CASE(const37); // ticket #2081 and #2085 TEST_CASE(constoperator1); // operator< can often be const TEST_CASE(constoperator2); // operator<< TEST_CASE(constoperator3); @@ -4046,6 +4047,36 @@ private: ASSERT_EQUALS("", errout.str()); } + void const37() // ticket #2081 and #2085 + { + checkConst("class A\n" + "{\n" + "public:\n" + " A(){};\n" + " std::string operator+(const char *c)\n" + " {\n" + " return m_str+std::string(c);\n" + " }\n" + "private:\n" + " std::string m_str;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (style) The function 'A::operator+' can be const\n", errout.str()); + + checkConst("class Fred\n" + "{\n" + "private:\n" + " long x;\n" + "public:\n" + " Fred() {\n" + " x = 0;\n" + " }\n" + " bool isValid() {\n" + " return bool(x == 0x11224488);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:9]: (style) The function 'Fred::isValid' can be const\n", errout.str()); + } + // increment/decrement => not const void constincdec() {