From e12d115e9aa84fc7c20f3d2304fe9c264f5c2ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 25 Jan 2010 21:40:57 +0100 Subject: [PATCH] Fixed #1312 (false positive: missing const message on functions returning references) --- lib/checkclass.cpp | 4 ++-- test/testclass.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 3aab8b818..04bfdf591 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1439,8 +1439,8 @@ void CheckClass::checkConst() continue; // member function? - if (Token::Match(tok2, "%type% *|&| %var% (") || - Token::Match(tok2, "%type% %type% *|&| %var% (") || + if (Token::Match(tok2, "%type% %var% (") || + Token::Match(tok2, "%type% %type% %var% (") || Token::Match(tok2, "%type% operator %any% (")) { // goto function name.. diff --git a/test/testclass.cpp b/test/testclass.cpp index 03433c94f..d3b0167ba 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -83,6 +83,7 @@ private: TEST_CASE(const1); TEST_CASE(constoperator); // operator< can often be const TEST_CASE(constincdec); // increment/decrement => non-const + TEST_CASE(constReturnReference); } // Check the operator Equal @@ -1586,6 +1587,17 @@ private: "};\n"); ASSERT_EQUALS("", errout.str()); } + + // return pointer/reference => not const + void constReturnReference() + { + checkConst("class Fred {\n" + " int a;\n" + " int &getR() { return a; }\n" + " int *getP() { return &a; }" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestClass)