diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 76d87feba..7f90adee1 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2221,6 +2221,13 @@ bool CheckClass::checkConstFunc(const SpaceInfo *info, const Token *tok) isconst = false; break; } + + // FIXME: I assume that a member union/struct variable is assigned. + else if (Token::Match(tok1->tokAt(-2), ". %var%")) + { + isconst = false; + break; + } } // streaming: << diff --git a/test/testclass.cpp b/test/testclass.cpp index 1e7e5d37f..31e222551 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -157,6 +157,7 @@ private: TEST_CASE(constVirtualFunc); TEST_CASE(constIfCfg); // ticket #1881 - fp when there are #if TEST_CASE(constFriend); // ticket #1921 - fp for friend function + TEST_CASE(constUnion); // ticket #2111 - fp when there are union TEST_CASE(symboldatabase1); TEST_CASE(symboldatabase2); @@ -4409,6 +4410,21 @@ private: ASSERT_EQUALS("", errout.str()); } + void constUnion() // ticket #2111 + { + checkConst("class foo {\n" + "public:\n" + " union {\n" + " int i;\n" + " float f;\n" + " } d;\n" + " void setf(float x) {\n" + " d.f = x;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void symboldatabase1() { checkConst("namespace foo {\n"