fix #2595 (False positive Technically the member function 'A::foo' can be const)

This commit is contained in:
Robert Reif 2011-02-20 20:01:54 -05:00
parent 763763fa9b
commit 5984b6b53f
2 changed files with 23 additions and 1 deletions

View File

@ -1351,7 +1351,7 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok)
{ {
const Token *tok1 = tok; const Token *tok1 = tok;
while (tok->previous() && !Token::Match(tok->previous(), "}|{|;|public:|protected:|private:|return|:|?")) while (tok->previous() && !Token::Match(tok->previous(), "}|{|;(||public:|protected:|private:|return|:|?"))
{ {
if (Token::simpleMatch(tok->previous(), "* this")) if (Token::simpleMatch(tok->previous(), "* this"))
return true; return true;
@ -1489,6 +1489,12 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok)
isconst = false; isconst = false;
break; break;
} }
else if (Token::simpleMatch(tok1->previous(), ") <<") &&
isMemberVar(scope, tok1->tokAt(-2)))
{
isconst = false;
break;
}
// increment/decrement (member variable?).. // increment/decrement (member variable?)..
else if (Token::Match(tok1, "++|--")) else if (Token::Match(tok1, "++|--"))

View File

@ -164,6 +164,7 @@ private:
TEST_CASE(const41); // ticket #2255 TEST_CASE(const41); // ticket #2255
TEST_CASE(const42); // ticket #2282 TEST_CASE(const42); // ticket #2282
TEST_CASE(const43); // ticket #2377 TEST_CASE(const43); // ticket #2377
TEST_CASE(const44); // ticket #2595
TEST_CASE(assigningPointerToPointerIsNotAConstOperation); TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
TEST_CASE(assigningArrayElementIsNotAConstOperation); TEST_CASE(assigningArrayElementIsNotAConstOperation);
TEST_CASE(constoperator1); // operator< can often be const TEST_CASE(constoperator1); // operator< can often be const
@ -4981,6 +4982,21 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void const44() // ticket 2595
{
checkConst("class A\n"
"{\n"
"public:\n"
" bool bOn;\n"
" bool foo()\n"
" {\n"
" return 0 != (bOn = bOn && true);\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void assigningPointerToPointerIsNotAConstOperation() void assigningPointerToPointerIsNotAConstOperation()
{ {
checkConst("struct s\n" checkConst("struct s\n"