Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `RULE-15-1-4` - `NonStaticMemberNotInitBeforeUse.ql`:
- Remove false positives for constructors of uninstantiated class templates, where delegating constructors are not modelled as such. The instantiations are still checked.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class IncompleteConstructor extends Constructor, IncompleteInitialization {
not getDeclaringType() instanceof Union and
this.getDeclaringType().hasDefinition() and
not this.isDeleted() and
// Constructors of uninstantiated class templates cannot be analysed reliably, because for
// instance a delegating constructor is not modelled as such. The instantiations are checked.
not this.getDeclaringType() instanceof TemplateClass and
// Delegating constructors do not need to initialize members
not any(ConstructorDelegationInit init) = this.getAnInitializer() and
// exclude defaulted move and copy constructors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
| test.cpp:267:18:267:18 | a | Aggregate variable does not initialize non-static data member $@ (and 9 more). | test.cpp:262:43:262:45 | m10 | m10 |
| test.cpp:290:18:290:18 | a | Aggregate variable does not initialize non-static data member $@ (and 1 more). | test.cpp:285:9:285:10 | m1 | m1 |
| test.cpp:290:18:290:18 | a | Aggregate variable does not initialize non-static data member $@ (and 1 more). | test.cpp:286:11:286:12 | m2 | m2 |
| test.cpp:318:3:318:3 | TemplateMemberUninit | Constructor does not initialize non-static data member $@. | test.cpp:322:7:322:8 | m2 | m2 |
50 changes: 49 additions & 1 deletion cpp/misra/test/rules/RULE-15-1-4/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,52 @@ union UnionWithConstructor {
// Union constructors should still be required to initialize one member, but
// this is not implemented.
UnionWithConstructor() {} // NON_COMPLIANT[False negative]
};
};
template <typename T> class TemplateAllInit {
public:
TemplateAllInit(T value) : m1(value), m2(0) {} // COMPLIANT
TemplateAllInit(const TemplateAllInit &) = delete;

private:
T m1;
int m2;
};

template <typename T> class TemplateMemberUninit {
public:
TemplateMemberUninit(T value) : m1(value) {} // NON_COMPLIANT

private:
T m1;
int m2;
};

template <typename T> class TemplateOutOfLineInit {
public:
TemplateOutOfLineInit(T value);

private:
T m1;
int m2;
};

template <typename T>
TemplateOutOfLineInit<T>::TemplateOutOfLineInit(T value)
: m1(value), m2(0) {} // COMPLIANT

template <typename T> class TemplateDelegating {
public:
TemplateDelegating() : TemplateDelegating(0) {} // COMPLIANT
TemplateDelegating(T value) : m1(value), m2(0) {} // COMPLIANT

private:
T m1;
int m2;
};

void instantiateTemplates() {
TemplateAllInit<int> a(1);
TemplateMemberUninit<int> b(1);
TemplateOutOfLineInit<int> c(1);
TemplateDelegating<int> d;
}
Loading