From 5dfbb38dc9ef5590d0e2cefbda89624cb2d38155 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Thu, 20 May 2010 06:52:59 +0200 Subject: [PATCH] #1697 (false positive: The function can be const) --- lib/checkclass.cpp | 12 ++++-------- test/testclass.cpp | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index d6dc8bcb8..9d61cd552 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -145,15 +145,11 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo // Pointer? else if (Token::Match(next, "%type% * %var% ;")) - { varname = next->strAt(2); - } - - // Pointer? else if (Token::Match(next, "%type% %type% * %var% ;")) - { varname = next->strAt(3); - } + else if (Token::Match(next, "%type% :: %type% * %var% ;")) + varname = next->strAt(4); // Array? else if (Token::Match(next, "%type% %var% [") && next->next()->str() != "operator") @@ -167,9 +163,9 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo // Pointer array? else if (Token::Match(next, "%type% * %var% [")) - { varname = next->strAt(2); - } + else if (Token::Match(next, "%type% :: %type% * %var% [")) + varname = next->strAt(4); // std::string.. else if (withClasses && Token::Match(next, "%type% :: %type% %var% ;")) diff --git a/test/testclass.cpp b/test/testclass.cpp index 9b0430192..8cc326f3e 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -117,6 +117,7 @@ private: TEST_CASE(const19); // ticket #1612 TEST_CASE(const20); // ticket #1602 TEST_CASE(const21); // ticket #1683 + TEST_CASE(const22); TEST_CASE(constoperator1); // operator< can often be const TEST_CASE(constoperator2); // operator<< TEST_CASE(constincdec); // increment/decrement => non-const @@ -3309,6 +3310,27 @@ private: ASSERT_EQUALS("", errout.str()); } + void const22() + { + checkConst("class A\n" + "{\n" + "private:\n" + " B::C * v1;\n" + "public:\n" + " void f1() { v1 = 0; }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + + checkConst("class A\n" + "{\n" + "private:\n" + " B::C * v1[0];\n" + "public:\n" + " void f1() { v1[0] = 0; }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } + // increment/decrement => not const void constincdec() {