diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 37eee7fbc..5994f2fdc 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1441,6 +1441,8 @@ void CheckClass::checkConst() // member function? if (Token::Match(tok2, "%type% %var% (") || Token::Match(tok2, "%type% %type% %var% (") || + Token::Match(tok2, "const %type% &|* %var% (") || + Token::Match(tok2, "const std :: %type% &|*| %var% (") || Token::Match(tok2, "%type% operator %any% (")) { // goto function name.. diff --git a/test/testclass.cpp b/test/testclass.cpp index 58551c2d7..e495f72d4 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -1546,6 +1546,17 @@ private: "};\n"); ASSERT_EQUALS("[test.cpp:3]: (style) The function 'Fred::getA' can be const\n", errout.str()); + checkConst("class Fred {\n" + " const std::string foo() { return ""; }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) The function 'Fred::foo' can be const\n", errout.str()); + + checkConst("class Fred {\n" + " std::string s;\n" + " const std::string & foo() { return ""; }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) The function 'Fred::foo' can be const\n", errout.str()); + // constructors can't be const.. checkConst("class Fred {\n" " int a;\n"