diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 6e127b06c..3aab8b818 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1433,8 +1433,14 @@ void CheckClass::checkConst() if (tok2->isName() && tok2->str().find(":") != std::string::npos) tok2 = tok2->next(); + // static functions can't be const + // virtual functions may be non-const for a reason + if (Token::Match(tok2, "static|virtual")) + continue; + // member function? - if (Token::Match(tok2, "%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 dceb343bd..03433c94f 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -1556,6 +1556,15 @@ private: " void b() { a(); }\n" "};\n"); ASSERT_EQUALS("", errout.str()); + + // static functions can't be const.. + checkConst("class foo\n" + "{\n" + "public:\n" + " static unsigned get()\n" + " { return 0; }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); } // operator< can often be const