From 5a8f490f071e9291ce4c1db4d2fd37dc1ad1e528 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 27 Oct 2010 19:25:34 +0200 Subject: [PATCH] Fixed #2135 (False positive 'Can be const') --- lib/checkclass.cpp | 4 +++- test/testclass.cpp | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 7f90adee1..3022dce7c 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2098,6 +2098,8 @@ void CheckClass::checkConst() bool CheckClass::isMemberVar(const SpaceInfo *info, const Token *tok) { + const Token *tok1 = tok; + while (tok->previous() && !Token::Match(tok->previous(), "}|{|;|public:|protected:|private:|return|:|?")) { if (Token::Match(tok->previous(), "* this")) @@ -2109,7 +2111,7 @@ bool CheckClass::isMemberVar(const SpaceInfo *info, const Token *tok) if (tok->str() == "this") return true; - if (Token::Match(tok, "( * %var% ) [")) + if (Token::Match(tok, "( * %var% ) [") || (Token::Match(tok, "( * %var% ) <<") && tok1->next()->str() == "<<")) tok = tok->tokAt(2); // ignore class namespace diff --git a/test/testclass.cpp b/test/testclass.cpp index 31e222551..8a9a10cba 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -146,6 +146,7 @@ private: TEST_CASE(const35); // ticket #2001 TEST_CASE(const36); // ticket #2003 TEST_CASE(const37); // ticket #2081 and #2085 + TEST_CASE(const38); // ticket #2135 TEST_CASE(constoperator1); // operator< can often be const TEST_CASE(constoperator2); // operator<< TEST_CASE(constoperator3); @@ -4088,6 +4089,23 @@ private: ASSERT_EQUALS("[test.cpp:9]: (style) The function 'Fred::isValid' can be const\n", errout.str()); } + void const38() // ticket #2135 + { + checkConst("class Foo {\n" + "public:\n" + " ~Foo() { delete oArq; }\n" + " Foo(): oArq(new std::ofstream(\"...\")) {}\n" + " void MyMethod();\n" + "private:\n" + " std::ofstream *oArq;\n" + "};\n" + "void Foo::MyMethod()\n" + "{\n" + " (*oArq) << \"\";\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + // increment/decrement => not const void constincdec() {