File tree 3 files changed +34
-1
lines changed
3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change
1
+ ` M9-3-3 ` : ` MemberFunctionConstIfPossible.ql ` :
2
+ - Fix FP reported in 381. Omit member functions that return nonconst reference types.
Original file line number Diff line number Diff line change @@ -37,6 +37,13 @@ class NonConstMemberFunction extends MemberFunction {
37
37
NonConstMemberFunction ( ) { not this .hasSpecifier ( "const" ) }
38
38
}
39
39
40
+ /**
41
+ * References that are not const
42
+ */
43
+ class NonConstReferenceType extends ReferenceType {
44
+ NonConstReferenceType ( ) { not this .isConst ( ) }
45
+ }
46
+
40
47
/**
41
48
* `MemberFunction`s that are not const
42
49
* and not `Constructor`s ect as const constructors are
@@ -57,7 +64,9 @@ class ConstMemberFunctionCandidate extends NonConstMemberFunction {
57
64
this .hasDefinition ( ) and
58
65
// For uninstantiated templates we have only partial information that prevents us from determining
59
66
// if the candidate calls non-const functions. Therefore we exclude these.
60
- not this .isFromUninstantiatedTemplate ( _)
67
+ not this .isFromUninstantiatedTemplate ( _) and
68
+ // Cannot recommend const if it returns a non-const reference.
69
+ not this .getType ( ) instanceof NonConstReferenceType
61
70
}
62
71
63
72
/**
Original file line number Diff line number Diff line change @@ -193,3 +193,25 @@ void test_template() {
193
193
class Z3 {
194
194
void f (int ) = delete; // COMPLIANT
195
195
};
196
+
197
+ class Z4 {
198
+ public:
199
+ int values[128 ];
200
+ template <typename T>
201
+ void fill (const T &val) { // COMPLIANT[FALSE_NEGATIVE|TRUE_NEGATIVE] -
202
+ // exception not specified in the
203
+ // standard, we opt to not raise an issue because the template can be both
204
+ // compliant and non-compliant depending on instantiations.
205
+ for (auto &elem : values) {
206
+ elem = val;
207
+ }
208
+ }
209
+ constexpr int &front () noexcept { return values[0 ]; } // COMPLIANT
210
+ };
211
+
212
+ void fp_reported_in_381 () {
213
+ // added to test template initialization effects/lack thereof
214
+ Z4 z;
215
+ int i = z.front ();
216
+ z.fill (i);
217
+ }
You can’t perform that action at this time.
0 commit comments