From 7ce4825f950c8e9235a9deaec86c3428d7337031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 24 Jan 2010 18:26:39 +0100 Subject: [PATCH] Fixed #1308 (False positive: The function 'x' can be const for a static member function) --- lib/checkclass.cpp | 8 +++++++- test/testclass.cpp | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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