diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 7d2d0e628..349989a1b 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -121,7 +121,7 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo } // Is it a variable declaration? - if (Token::Match(next, "%type% %var% ;")) + if (Token::Match(next, "%type% %var% ;|:")) { if (withClasses) varname = next->strAt(1); @@ -1818,6 +1818,20 @@ bool CheckClass::isMemberFunc(const Token *tok) break; } } + + // check for templates returning pointers or references + else if (Token::Match(tok, "*|&")) + { + int back = -2; + if (tok->strAt(back) == "::") + back -= 2; + + if (tok->strAt(back) != "const") + { + if (!isConst) + return false; + } + } tok = tok->next(); } } diff --git a/test/testclass.cpp b/test/testclass.cpp index 4d153532c..ce0c10c14 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -114,6 +114,7 @@ private: TEST_CASE(const17); // ticket #1552 TEST_CASE(const18); // ticket #1563 TEST_CASE(const19); // ticket #1612 + TEST_CASE(const20); // ticket #1602 TEST_CASE(constoperator1); // operator< can often be const TEST_CASE(constoperator2); // operator<< TEST_CASE(constincdec); // increment/decrement => non-const @@ -3231,6 +3232,45 @@ private: ASSERT_EQUALS("", errout.str()); } + void const20() + { + // ticket #1602 + checkConst("class Fred {\n" + " int x : 3;\n" + "public:\n" + " void set(int i) { x = i; }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + + checkConst("class Fred {\n" + " list x;\n" + "public:\n" + " list get() { return x; }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + + checkConst("class Fred {\n" + " list x;\n" + "public:\n" + " list get() { return x; }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) The function 'Fred::get' can be const\n", errout.str()); + + checkConst("class Fred {\n" + " std::list x;\n" + "public:\n" + " std::list get() { return x; }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + + checkConst("class Fred {\n" + " std::list x;\n" + "public:\n" + " std::list get() { return x; }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) The function 'Fred::get' can be const\n", errout.str()); + } + // increment/decrement => not const void constincdec() {