From cc8cf9d3d0c4d784509ccef529c7688b6989efad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 31 Aug 2023 20:04:29 +0200 Subject: [PATCH 1/2] Fixed #11907 (False positive: uninitialized member (mutable member, const method call)) --- lib/checkclass.cpp | 8 ++++++++ test/testconstructors.cpp | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index dd15ca06641..db3ad57bbab 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -988,6 +988,14 @@ void CheckClass::initializeVarList(const Function &func, std::listisConst() && !member->isStatic()) { assignAllVar(usage); } + + // const method, assume it assigns all mutable members + else if (member->isConst()) { + for (Usage& usage: usage) { + if (usage.var->isMutable()) + usage.assign = true; + } + } } // not member function diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 55925648811..418025e33c9 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -116,6 +116,7 @@ class TestConstructors : public TestFixture { TEST_CASE(initvar_chained_assign); // BUG 2270433 TEST_CASE(initvar_2constructors); // BUG 2270353 TEST_CASE(initvar_constvar); + TEST_CASE(initvar_mutablevar); TEST_CASE(initvar_staticvar); TEST_CASE(initvar_brace_init); TEST_CASE(initvar_union); @@ -1194,6 +1195,18 @@ class TestConstructors : public TestFixture { } + void initvar_mutablevar() { + check("class Foo {\n" + "public:\n" + " Foo() { update(); }\n" + "private:\n" + " void update() const;\n" + " mutable int x;\n" + "};"); + ASSERT_EQUALS("", errout.str()); + } + + void initvar_staticvar() { check("class Fred\n" "{\n" From ed6f5bb2309579a4650c90d667398c3550d26c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 1 Sep 2023 12:45:40 +0200 Subject: [PATCH 2/2] shadow variable --- lib/checkclass.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index db3ad57bbab..49613c06914 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -991,9 +991,9 @@ void CheckClass::initializeVarList(const Function &func, std::listisConst()) { - for (Usage& usage: usage) { - if (usage.var->isMutable()) - usage.assign = true; + for (Usage& i: usage) { + if (i.var->isMutable()) + i.assign = true; } } }