From 2eceaaefc0e99cd82997a768510d2e88baf8acce Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 26 Mar 2010 20:14:31 +0100 Subject: [PATCH] Fixed #1519 (false negative: the function can be declared as const) --- lib/checkclass.cpp | 4 ++++ test/testclass.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 993056d4b..6b9ec6597 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1581,8 +1581,12 @@ void CheckClass::checkConst() if (Token::Match(tok2, "%type% %var% (") || Token::Match(tok2, "%type% %type% %var% (") || Token::Match(tok2, "%type% :: %type% %var% (") || + Token::Match(tok2, "%type% :: %type% < %type% > %var% (") || + Token::Match(tok2, "%type% :: %type% < %type% , %type% > %var% (") || Token::Match(tok2, "const %type% &|* %var% (") || Token::Match(tok2, "const %type% :: %type% &|*| %var% (") || + Token::Match(tok2, "const %type% :: %type% < %type% > *|& %var% (") || + Token::Match(tok2, "const %type% :: %type% < %type% , %type% > *|& %var% (") || Token::Match(tok2, "%type% operator %any% (")) { // goto function name.. diff --git a/test/testclass.cpp b/test/testclass.cpp index 36738f8eb..a0ea8acf9 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -99,6 +99,7 @@ private: TEST_CASE(const10); // ticket #1522 TEST_CASE(const11); // ticket #1529 TEST_CASE(const12); // ticket #1552 + TEST_CASE(const13); // ticket #1519 TEST_CASE(constoperator); // operator< can often be const TEST_CASE(constincdec); // increment/decrement => non-const TEST_CASE(constReturnReference); @@ -2242,6 +2243,34 @@ private: ASSERT_EQUALS("[test.cpp:3]: (style) The function 'A::foo' can be const\n", errout.str()); } + void const13() + { + // ticket #1519 + checkConst("class A {\n" + "public:\n" + " A(){}\n" + " std::vector GetVec() {return m_vec;}\n" + " std::pair GetPair() {return m_pair;}\n" + "private:\n" + " std::vector m_vec;\n" + " std::pair m_pair;\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (style) The function 'A::GetVec' can be const\n" + "[test.cpp:5]: (style) The function 'A::GetPair' can be const\n", errout.str()); + + checkConst("class A {\n" + "public:\n" + " A(){}\n" + " const std::vector & GetVec() {return m_vec;}\n" + " const std::pair & GetPair() {return m_pair;}\n" + "private:\n" + " std::vector m_vec;\n" + " std::pair m_pair;\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (style) The function 'A::GetVec' can be const\n" + "[test.cpp:5]: (style) The function 'A::GetPair' can be const\n", errout.str()); + } + // increment/decrement => not const void constincdec() {