Fixed #2135 (False positive 'Can be const')

This commit is contained in:
Robert Reif 2010-10-27 19:25:34 +02:00 committed by Daniel Marjamäki
parent 6649e31514
commit 5a8f490f07
2 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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) << \"</table>\";\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
// increment/decrement => not const
void constincdec()
{