Skip to content

Fix #15080 FP uninitMemberVarNoCtor for const and reference members - #8910

Open
autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-noctor-const-member
Open

autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-noctor-const-member

Conversation

@autoantwort

Copy link
Copy Markdown
Contributor

https://trac.cppcheck.net/ticket/15080

struct S {
    const std::array<std::uint8_t, 2> a;
    const std::vector<std::uint8_t>::const_iterator it;
};
S f(const std::vector<std::uint8_t>& v) {
    return S{ {}, v.cbegin() };
}

gives Member variable 'S::it' has no initializer. [uninitMemberVarNoCtor], and struct S { int a = 0; const int b; int& c; }; warns for b and c.

A non-static const or reference member without default member initializer can't stay uninitialized: for trivial types the implicit default constructor is deleted, so every instance must initialize it, and a const class type is default-constructed. These members are now skipped by the no-constructor check.

Since f90a76f (#15072) this also shows up for class templates whose member type is a using alias.

This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
Comment thread lib/checkclass.cpp
if (var.typeScope() && var.typeScope()->numConstructors > 0)
continue;

// const and reference members without default initializer must be initialized by every instance

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should be done in the loop above (see hasAnyDefaultInit etc.). A const/ref member should inhibit warnings for any other members as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants