fix #2663 (False negative: function can be const (changing unknown or uninitialised variable))

This commit is contained in:
Robert Reif 2011-03-20 13:29:52 -04:00
parent 41d80b5c8d
commit 2277cb6965
2 changed files with 29 additions and 1 deletions

View File

@ -1546,7 +1546,17 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok)
// increment/decrement (member variable?)..
else if (Token::Match(tok1, "++|--"))
{
isconst = false;
if (Token::Match(tok1->previous(), "%var%") &&
tok1->previous()->str() != "return")
{
if (isMemberVar(scope, tok1->previous()))
isconst = false;
}
else if (Token::Match(tok1->next(), "%var%"))
{
if (isMemberVar(scope, tok1->next()))
isconst = false;
}
break;
}

View File

@ -166,6 +166,7 @@ private:
TEST_CASE(const43); // ticket #2377
TEST_CASE(const44); // ticket #2595
TEST_CASE(const45); // ticket #2664
TEST_CASE(const46); // ticket #2636
TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
TEST_CASE(assigningArrayElementIsNotAConstOperation);
TEST_CASE(constoperator1); // operator< can often be const
@ -5210,6 +5211,23 @@ private:
ASSERT_EQUALS("[test.cpp:8]: (information) Technically the member function 'tools::WorkspaceControl::toGrid' can be const.\n", errout.str());
}
void const46() // ticket 2663
{
checkConst("class Altren {\n"
"public:\n"
" int fun1() {\n"
" int a;\n"
" a++;\n"
" }\n"
" int fun2() {\n"
" b++;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (information) Technically the member function 'Altren::fun1' can be const.\n"
"[test.cpp:7]: (information) Technically the member function 'Altren::fun2' can be const.\n", errout.str());
}
void assigningPointerToPointerIsNotAConstOperation()
{
checkConst("struct s\n"