diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ef52024db..91763c6a8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1299,6 +1299,10 @@ static bool isVariableMutableInInitializer(const Token* start, const Token * end const Token * memberTok = tok->astParent()->previous(); if (Token::Match(memberTok, "%var% (") && memberTok->variable()) { const Variable * memberVar = memberTok->variable(); + if(memberVar->isClass()) + //TODO: check if the called constructor could live with a const variable + // pending that, assume the worst (that it can't) + return true; if (!memberVar->isReference()) continue; if (memberVar->isConst()) diff --git a/test/testother.cpp b/test/testother.cpp index 6bbf2bc37..715a5a6ac 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2107,6 +2107,90 @@ private: "void an();\n" "void h();\n"); ASSERT_EQUALS("", errout.str()); + + check("class C\n" + "{\n" + "public:\n" + " explicit C(int&);\n" + "};\n" + "\n" + "class D\n" + "{\n" + "public:\n" + " explicit D(int&);\n" + "\n" + "private:\n" + " C c;\n" + "};\n" + "\n" + "D::D(int& i)\n" + " : c(i)\n" + "{\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("class C\n" + "{\n" + "public:\n" + " explicit C(const int&);\n" + "};\n" + "\n" + "class D\n" + "{\n" + "public:\n" + " explicit D(int&);\n" + "\n" + "private:\n" + " C c;\n" + "};\n" + "\n" + "D::D(int& i)\n" + " : c(i)\n" + "{\n" + "}\n"); + TODO_ASSERT_EQUALS("[test.cpp:16]: (style) Parameter 'i' can be declared with const\n", "", errout.str()); + + check("class C\n" + "{\n" + "public:\n" + " explicit C(int);\n" + "};\n" + "\n" + "class D\n" + "{\n" + "public:\n" + " explicit D(int&);\n" + "\n" + "private:\n" + " C c;\n" + "};\n" + "\n" + "D::D(int& i)\n" + " : c(i)\n" + "{\n" + "}\n"); + TODO_ASSERT_EQUALS("[test.cpp:16]: (style) Parameter 'i' can be declared with const\n", "", errout.str()); + + check("class C\n" + "{\n" + "public:\n" + " explicit C(int, int);\n" + "};\n" + "\n" + "class D\n" + "{\n" + "public:\n" + " explicit D(int&);\n" + "\n" + "private:\n" + " C c;\n" + "};\n" + "\n" + "D::D(int& i)\n" + " : c(0, i)\n" + "{\n" + "}\n"); + TODO_ASSERT_EQUALS("[test.cpp:16]: (style) Parameter 'i' can be declared with const\n", "", errout.str()); } void switchRedundantAssignmentTest() {