File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Expand file tree Collapse file tree 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 {
3737 NonConstMemberFunction ( ) { not this .hasSpecifier ( "const" ) }
3838}
3939
40+ /**
41+ * References that are not const
42+ */
43+ class NonConstReferenceType extends ReferenceType {
44+ NonConstReferenceType ( ) { not this .isConst ( ) }
45+ }
46+
4047/**
4148 * `MemberFunction`s that are not const
4249 * and not `Constructor`s ect as const constructors are
@@ -57,7 +64,9 @@ class ConstMemberFunctionCandidate extends NonConstMemberFunction {
5764 this .hasDefinition ( ) and
5865 // For uninstantiated templates we have only partial information that prevents us from determining
5966 // 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
6170 }
6271
6372 /**
Original file line number Diff line number Diff line change @@ -193,3 +193,25 @@ void test_template() {
193193class Z3 {
194194 void f (int ) = delete; // COMPLIANT
195195};
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