const: fixed false positive when using increment/decrement

This commit is contained in:
Daniel Marjamäki 2010-01-24 13:33:30 +01:00
parent 24052c16b4
commit b4b97e5706
2 changed files with 20 additions and 2 deletions

View File

@ -1470,6 +1470,13 @@ void CheckClass::checkConst()
break;
}
// increment/decrement (member variable?)..
else if (Token::Match(tok3, "++|--"))
{
isconst = false;
break;
}
// function call..
else if (tok3->str() != "return" && Token::Match(tok3, "%var% ("))
{

View File

@ -81,7 +81,8 @@ private:
// can member function be made const
TEST_CASE(const1);
TEST_CASE(const2);
TEST_CASE(constoperator); // operator< can often be const
TEST_CASE(constincdec); // increment/decrement => non-const
}
// Check the operator Equal
@ -1558,7 +1559,7 @@ private:
}
// operator< can often be const
void const2()
void constoperator()
{
checkConst("struct Fred {\n"
" int a;\n"
@ -1566,6 +1567,16 @@ private:
"};\n");
ASSERT_EQUALS("[test.cpp:3]: (style) The function 'Fred::operator<' can be const\n", errout.str());
}
// increment/decrement => not const
void constincdec()
{
checkConst("class Fred {\n"
" int a;\n"
" void nextA() { return ++a; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestClass)