Skip to content

Commit c6fb3af

Browse files
authored
Merge pull request #570 from knewbury01/knewbury01/fix-381
M9-3-3: address fp issue 381
2 parents a0d9eee + f9a503b commit c6fb3af

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`M9-3-3`: `MemberFunctionConstIfPossible.ql`:
2+
- Fix FP reported in 381. Omit member functions that return nonconst reference types.

cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql

+10-1
Original file line numberDiff line numberDiff 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
/**

cpp/autosar/test/rules/M9-3-3/test.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,25 @@ void test_template() {
193193
class 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+
}

0 commit comments

Comments
 (0)