Skip to content

Commit

Permalink
Fixed #1527 (Function can't be const if it has non-const operator ove…
Browse files Browse the repository at this point in the history
…rload call)
  • Loading branch information
Daniel Marjamäki committed Apr 2, 2010
1 parent 93d4851 commit 427d155
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,13 @@ bool CheckClass::checkConstFunc(const std::string &classname, const Var *varlist
}
}

// streaming: <<
else if (tok1->str() == "<<" && isMemberVar(classname, varlist, tok1->previous()))
{
isconst = false;
break;
}

// increment/decrement (member variable?)..
else if (Token::Match(tok1, "++|--"))
{
Expand Down
21 changes: 19 additions & 2 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ class TestClass : public TestFixture
TEST_CASE(const16); // ticket #1551
TEST_CASE(const17); // ticket #1552
TEST_CASE(const18); // ticket #1563
TEST_CASE(constoperator); // operator< can often be const
TEST_CASE(constoperator1); // operator< can often be const
TEST_CASE(constoperator2); // operator<<
TEST_CASE(constincdec); // increment/decrement => non-const
TEST_CASE(constReturnReference);
TEST_CASE(constDelete); // delete member variable => not const
Expand Down Expand Up @@ -2241,7 +2242,7 @@ class TestClass : public TestFixture
}

// operator< can often be const
void constoperator()
void constoperator1()
{
checkConst("struct Fred {\n"
" int a;\n"
Expand All @@ -2250,6 +2251,22 @@ class TestClass : public TestFixture
ASSERT_EQUALS("[test.cpp:3]: (style) The function 'Fred::operator<' can be const\n", errout.str());
}

// operator<<
void constoperator2()
{
checkConst("struct Foo {\n"
" void operator<<(int);\n"
"};\n"
"struct Fred {\n"
" Foo foo;\n"
" void x()\n"
" {\n"
" foo << 123;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}

void const5()
{
// ticket #1482
Expand Down

0 comments on commit 427d155

Please sign in to comment.