Repository navigation
Fix #15080 FP uninitMemberVarNoCtor for const and reference members - #8910
autoantwort wants to merge 2 commits into
Conversation
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>
…or for all members Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Any further changes requested? |
|
@claude review |
| if (var.hasDefault()) { | ||
| usage.init = true; | ||
| hasAnyDefaultInit = true; | ||
| } else if (var.isReference() || (var.isConst() && !var.isClass())) { |
There was a problem hiding this comment.
This branch also catches static members. createUsageList() keeps static members because isInitialized() filters them out later, so a static const or static reference member without an in-class initializer now marks the class as not default constructible. That suppresses every warning for the class, which is a new false negative:
struct S {
int a = 0;
static const int N; // defined out of class
int k; // warned before this PR, not anymore
};Suggest excluding static members, and adding a test for this case:
| } else if (var.isReference() || (var.isConst() && !var.isClass())) { | |
| } else if (!var.isStatic() && (var.isReference() || (var.isConst() && !var.isClass()))) { |
|
The approach makes sense: a const member of trivial type, or a reference member, with no initializer deletes the implicit default constructor, so every instance has to be initialized another way. One issue (inline): static const and static reference members are in the usage list too. The new check doesn't exclude them, so a declaration like Minor: it would be good to add a test for a const pointer member ( 🤖 Generated with Claude Code |
|
please check the results here: |
https://trac.cppcheck.net/ticket/15080
gives
Member variable 'S::it' has no initializer. [uninitMemberVarNoCtor], andstruct S { int a = 0; const int b; int& c; };warns forbandc.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
usingalias.This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.
🤖 Generated with Claude Code