diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index ab610bc75..8607ce205 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -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, "++|--")) { diff --git a/test/testclass.cpp b/test/testclass.cpp index 6bbaee42f..f65a2e91e 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -112,7 +112,8 @@ private: 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 @@ -2241,7 +2242,7 @@ private: } // operator< can often be const - void constoperator() + void constoperator1() { checkConst("struct Fred {\n" " int a;\n" @@ -2250,6 +2251,22 @@ private: 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