Skip to content

Implement Rule 28-6-4, require use of std::remove_if and similar functions#1046

Open
MichaelRFairhurst wants to merge 4 commits intomainfrom
michaelrfairhurst/implement-deadcode-11-use-result-of-remove-algorithms-etc
Open

Implement Rule 28-6-4, require use of std::remove_if and similar functions#1046
MichaelRFairhurst wants to merge 4 commits intomainfrom
michaelrfairhurst/implement-deadcode-11-use-result-of-remove-algorithms-etc

Conversation

@MichaelRFairhurst
Copy link
Collaborator

Description

Implement Rule 28-6-4

Change request type

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql, .qll, .qls or unit tests)
  • External scripts (analysis report or other code shipped as part of a release)

Rules with added or modified queries

  • No rules added
  • Queries have been added for the following rules:
    • RULE-28-6-4
  • Queries have been modified for the following rules:
    • rule number here

Release change checklist

A change note (development_handbook.md#change-notes) is required for any pull request which modifies:

  • The structure or layout of the release artifacts.
  • The evaluation performance (memory, execution time) of an existing query.
  • The results of an existing query in any circumstance.

If you are only adding new rule queries, a change note is not required.

Author: Is a change note required?

  • Yes
  • No

🚨🚨🚨
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.

  • Confirmed

Reviewer: Confirm that either a change note is not required or the change note is required and has been added.

  • Confirmed

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

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Reviewer

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Copilot AI review requested due to automatic review settings February 18, 2026 22:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DeadCode11 query 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::empty and 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.

Comment on lines 1 to 3
import cpp
import codingstandards.cpp.misra

Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
 */

Copilot uses AI. Check for mistakes.
Comment on lines 5 to 10
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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
)
)

Copilot uses AI. Check for mistakes.
Comment on lines 13 to 17
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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)

Copilot uses AI. Check for mistakes.
from FunctionCall fc, string message
where
not isExcluded(fc, DeadCode11Package::potentiallyErroneousContainerUsageQuery()) and
exists(ExprStmt es | es.getExpr() = fc) and
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
exists(ExprStmt es | es.getExpr() = fc) and
not fc.isResultUsed() and

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments