Skip to content

Commit

Permalink
M9-3-3: address fp issue 381
Browse files Browse the repository at this point in the history
  • Loading branch information
knewbury01 committed Apr 12, 2024
1 parent b31377d commit a9e6e83
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions change_notes/2024-04-12-fix-fp-m9-3-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`M9-3-3`: `MemberFunctionConstIfPossible.ql`:
- Fix FP reported in 381. Omit member functions that return nonconst reference types.
11 changes: 10 additions & 1 deletion cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class NonConstMemberFunction extends MemberFunction {
NonConstMemberFunction() { not this.hasSpecifier("const") }
}

/**
* References that are not const
*/
class NonConstReferenceType extends ReferenceType {
NonConstReferenceType() { not this.isConst() }
}

/**
* `MemberFunction`s that are not const
* and not `Constructor`s ect as const constructors are
Expand All @@ -57,7 +64,9 @@ class ConstMemberFunctionCandidate extends NonConstMemberFunction {
this.hasDefinition() and
// For uninstantiated templates we have only partial information that prevents us from determining
// if the candidate calls non-const functions. Therefore we exclude these.
not this.isFromUninstantiatedTemplate(_)
not this.isFromUninstantiatedTemplate(_) and
//cannot recommend const if it returns a nonconst reference
not this.getType() instanceof NonConstReferenceType
}

/**
Expand Down
21 changes: 21 additions & 0 deletions cpp/autosar/test/rules/M9-3-3/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,24 @@ void test_template() {
class Z3 {
void f(int) = delete; // COMPLIANT
};

class Z4 {
public:
int values[128];
template <typename T>
void fill(const T &val) { // COMPLIANT[FALSE_NEGATIVE|TRUE_NEGATIVE] -
// exception not specified in the
// standard, we opt to not raise an issue because the template can be both
// compliant and non-compliant depending on instantiations.
for (auto &elem : values) {
elem = val;
}
}
constexpr int &front() noexcept { return values[0]; } // COMPLIANT
};

void fp_reported_in_381() {
Z4 z;
int i = z.front();
z.fill(i);
}

0 comments on commit a9e6e83

Please sign in to comment.