Skip to content

M9-3-3: address fp issue 381 #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2024
Merged
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
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 non-const reference.
not this.getType() instanceof NonConstReferenceType
}

/**
Expand Down
22 changes: 22 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,25 @@ 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() {
// added to test template initialization effects/lack thereof
Z4 z;
int i = z.front();
z.fill(i);
}
Loading