Implement Rule 28-6-4, require use of std::remove_if and similar functions#1046
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new MISRA C++:2023 query implementation for RULE-28-6-4 (requiring the return value of std::remove, std::remove_if, std::unique, and empty to be used), wiring it into the rule packaging/exclusions framework and providing unit tests plus supporting standard-library test stubs.
Changes:
- Registers RULE-28-6-4 under a new
DeadCode11query package and hooks it into the exclusions metadata system. - Adds the RULE-28-6-4 query (
PotentiallyErroneousContainerUsage.ql) and corresponding unit tests/expected results. - Extends the C++ standard-library test stubs to declare
std::emptyand required container/algorithm APIs used by tests.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| rules.csv | Switches RULE-28-6-4 to the new DeadCode11 package. |
| rule_packages/cpp/DeadCode11.json | Declares package metadata for RULE-28-6-4 query. |
| cpp/misra/src/rules/RULE-28-6-4/PotentiallyErroneousContainerUsage.ql | Implements the query logic for detecting unused results. |
| cpp/misra/test/rules/RULE-28-6-4/test.cpp | Adds compliant/non-compliant test cases. |
| cpp/misra/test/rules/RULE-28-6-4/PotentiallyErroneousContainerUsage.qlref | Hooks the test to the query. |
| cpp/misra/test/rules/RULE-28-6-4/PotentiallyErroneousContainerUsage.expected | Adds expected findings. |
| cpp/common/test/includes/standard-library/* | Updates/introduces test stubs for std::empty, erase, and algorithm declarations. |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll | Registers the new DeadCode11 package in the query-metadata union. |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode11.qll | Adds the autogenerated DeadCode11 exclusion/metadata module. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import cpp | ||
| import codingstandards.cpp.misra | ||
|
|
There was a problem hiding this comment.
This query file is missing the standard CodeQL metadata header (e.g. @id, @name, @description, severity/tags). The exclusions metadata in DeadCode11.qll expects an @id of cpp/misra/potentially-erroneous-container-usage, so without metadata this query may not be packaged/identified correctly and may break rule metadata generation.
See below for a potential fix:
@ -1,3 +1,14 @@
/**
* @id cpp/misra/potentially-erroneous-container-usage
* @name MISRA C++ 2008 rule 28-6-4 - Potentially erroneous container usage
* @description Flags calls to standard library container-related functions such as
* std::remove, std::remove_if, std::unique, or empty where the
* result of the call is not used.
* @kind problem
* @problem.severity warning
* @precision medium
* @tags cpp misra reliability maintainability
*/
There was a problem hiding this comment.
isRemoveOrUniqueCall matches any std::remove by name/namespace, which can include non-algorithm overloads like std::remove(const char*) from <cstdio> (and potentially other remove functions). This will create false positives unrelated to the iterator-returning algorithm. Consider constraining to the algorithm overload set (e.g., by checking parameter count/types/return type) rather than just the unqualified name.
| exists(string name | name = fc.getTarget().getName() | | |
| name = "remove" or | |
| name = "remove_if" or | |
| name = "unique" | |
| ) and | |
| fc.getTarget().hasQualifiedName("std", _) | |
| exists(Function f, string name | | |
| f = fc.getTarget() and | |
| f.hasQualifiedName("std", _) and | |
| name = f.getName() and | |
| ( | |
| name = "remove" and f.getNumberOfParameters() >= 2 or | |
| name = "remove_if" and f.getNumberOfParameters() >= 2 or | |
| name = "unique" and f.getNumberOfParameters() >= 2 | |
| ) | |
| ) |
There was a problem hiding this comment.
isEmptyCall currently flags any member function named empty (instanceof MemberFunction) regardless of the declaring type/namespace. That will report on user-defined APIs like MyType::empty() even though RULE-28 is about standard library usage. Restrict this to standard-library containers (e.g., member functions whose declaring type is in std / recognized STL container types) plus std::empty.
| predicate isEmptyCall(FunctionCall fc) { | |
| fc.getTarget().getName() = "empty" and | |
| ( | |
| fc.getTarget().hasQualifiedName("std", "empty") or | |
| fc.getTarget() instanceof MemberFunction | |
| /** Returns true if `fc` is a call to a member `empty` function of a type in namespace `std`. */ | |
| predicate isStdContainerEmptyMember(FunctionCall fc) { | |
| fc.getTarget() instanceof MemberFunction and | |
| fc.getTarget().getName() = "empty" and | |
| exists(Type t | | |
| fc.getTarget().getDeclaringType() = t and | |
| t.getNamespace().getName() = "std" | |
| ) | |
| } | |
| predicate isEmptyCall(FunctionCall fc) { | |
| fc.getTarget().getName() = "empty" and | |
| ( | |
| fc.getTarget().hasQualifiedName("std", "empty") or | |
| isStdContainerEmptyMember(fc) |
| from FunctionCall fc, string message | ||
| where | ||
| not isExcluded(fc, DeadCode11Package::potentiallyErroneousContainerUsageQuery()) and | ||
| exists(ExprStmt es | es.getExpr() = fc) and |
There was a problem hiding this comment.
The unused-result check only matches when the call expression is exactly the expression statement (es.getExpr() = fc). This misses common “discard result” patterns like static_cast<void>(std::remove(...)) or wrapping the call in a comma operator/parentheses. If the rule intends to catch deliberate discards, consider using a more general predicate for “return value is ignored” that also covers cast-to-void and similar wrappers.
| exists(ExprStmt es | es.getExpr() = fc) and | |
| not fc.isResultUsed() and |
There was a problem hiding this comment.
Incorrect
Description
Implement Rule 28-6-4
Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
RULE-28-6-4Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
Query development review checklist
For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:
Author
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
Reviewer
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.