From ba74606e7d4decf5c9ff29876639b3fc7771e0fe Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Wed, 30 Sep 2026 21:29:32 +0200 Subject: [PATCH] Fix #15080 FP uninitMemberVarNoCtor for const and reference members A class without constructor whose non-static const or reference member has no default member initializer is not default constructible (or, for a const class type with a default constructor, default constructs it), so the member can never be left uninitialized. Co-Authored-By: Claude Opus 5.5 --- lib/checkclass.cpp | 4 ++++ test/testconstructors.cpp | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 314af14e158..fdacd5c270a 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -373,6 +373,10 @@ void CheckClassImpl::constructors() if (var.typeScope() && var.typeScope()->numConstructors > 0) continue; + // const and reference members without default initializer must be initialized by every instance + if (var.isConst() || var.isReference()) + continue; + if (diagVars.count(&var) == 0) uninitVarError(var.nameToken(), false, FunctionType::eConstructor, var.scope()->className, var.name(), false, false, true); } diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 4de49f54311..deb0ce07fce 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -797,6 +797,23 @@ class TestConstructors : public TestFixture { ASSERT_EQUALS("[test.cpp:4:9]: (warning) Member variable 'T::j' has no initializer. [uninitMemberVarNoCtor]\n" "[test.cpp:8:9]: (warning) Member variable 'U::k' has no initializer. [uninitMemberVarNoCtor]\n", errout_str()); + + check("struct S {\n" // const and reference members can't stay uninitialized + " int a = 0;\n" + " const int b;\n" + " int& c;\n" + " const int* d;\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:5:16]: (warning) Member variable 'S::d' has no initializer. [uninitMemberVarNoCtor]\n", errout_str()); + + check("struct S {\n" + " const std::array a;\n" + " const std::vector::const_iterator it;\n" + "};\n" + "S f(const std::vector& v) {\n" + " return S{ {}, v.cbegin() };\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } // ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."