From 217b9425b5dd790089f7fc33968bf849ff80fd4d Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Mon, 9 Aug 2010 17:54:16 +0200 Subject: [PATCH] Fixed #1922 (False positive: function can be const when both const and non-const functions are provided.) --- lib/checkclass.cpp | 2 +- test/testclass.cpp | 57 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 28776dcf5..4b8e90e79 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -229,7 +229,7 @@ void CheckClass::createSymbolDatabase() while (found->next()->str() != "(") found = found->next(); - if (Token::Match(found->next()->link(), ") const| {")) + if (Token::Match(found->next()->link(), function.isConst ? ") const {" : ") {")) { if (argsMatch(funcArgs, found->tokAt(2), classPath, depth)) { diff --git a/test/testclass.cpp b/test/testclass.cpp index a57c76111..63743a08c 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -133,6 +133,7 @@ private: TEST_CASE(const26); // ticket #1847 TEST_CASE(const27); // ticket #1882 TEST_CASE(const28); // ticket #1883 + TEST_CASE(const29); // ticket #1922 TEST_CASE(constoperator1); // operator< can often be const TEST_CASE(constoperator2); // operator<< TEST_CASE(constincdec); // increment/decrement => non-const @@ -3629,6 +3630,26 @@ private: ASSERT_EQUALS("", errout.str()); } + void const27() // ticket #1882 + { + checkConst("class A {\n" + "public:\n" + " A(){m_d=1.0; m_iRealVal=2.0;}\n" + " double dGetValue();\n" + "private:\n" + " double m_d;\n" + " double m_iRealVal;\n" + "};\n" + "double A::dGetValue() {\n" + " double dRet = m_iRealVal;\n" + " if( m_d != 0 )\n" + " return dRet / m_d;\n" + " return dRet;\n" + "};\n" + ); + ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:4]: (style) The function 'A::dGetValue' can be const\n", errout.str()); + } + void const28() // ticket #1883 { checkConst("class P {\n" @@ -3669,24 +3690,30 @@ private: } - void const27() // ticket #1882 + void const29() // ticket #1922 { - checkConst("class A {\n" - "public:\n" - " A(){m_d=1.0; m_iRealVal=2.0;}\n" - " double dGetValue();\n" - "private:\n" - " double m_d;\n" - " double m_iRealVal;\n" - "};\n" - "double A::dGetValue() {\n" - " double dRet = m_iRealVal;\n" - " if( m_d != 0 )\n" - " return dRet / m_d;\n" - " return dRet;\n" + checkConst("class test {\n" + " public:\n" + " test();\n" + " const char* get() const;\n" + " char* get();\n" + " private:\n" + " char* value_;\n" "};\n" + "test::test()\n" + "{\n" + " value_ = 0;\n" + "}\n" + "const char* test::get() const\n" + "{\n" + " return value_;\n" + "}\n" + "char* test::get()\n" + "{\n" + " return value_;\n" + "}\n" ); - ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:4]: (style) The function 'A::dGetValue' can be const\n", errout.str()); + ASSERT_EQUALS("", errout.str()); } // increment/decrement => not const