Michaelrfairhurst/declarations 10-1-1 properly qualify lvalue refs pointers#1102
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new MISRA C++:2023 RULE-10-1-1 query that flags pointer/lvalue-reference parameters whose pointee/referent is not modified and therefore can be const-qualified, along with a dedicated test suite and supporting metadata/packaging updates.
Changes:
- Added new query implementation
PointerOrRefParamNotConst.qlforRULE-10-1-1. - Added unit tests for the rule, including
.qlrefand.expectedoutputs. - Added a new rule package descriptor (
Declarations6) and wired it into the exclusions metadata; updated test stdlibvectormodel for const-correctcbegin/cend.
Show a summary per file
| File | Description |
|---|---|
| rules.csv | Updates rule-to-package mapping for RULE-10-1-1 (currently points to the wrong package). |
| rule_packages/cpp/Declarations6.json | New rule package description file for RULE-10-1-1. |
| cpp/misra/src/rules/RULE-10-1-1/PointerOrRefParamNotConst.ql | New CodeQL query implementing the rule (contains wiring + cleanup/doc issues). |
| cpp/misra/test/rules/RULE-10-1-1/test.cpp | New test cases for compliant/non-compliant parameter usage patterns. |
| cpp/misra/test/rules/RULE-10-1-1/PointerOrRefParamNotConst.qlref | Test query reference to the shipped query. |
| cpp/misra/test/rules/RULE-10-1-1/PointerOrRefParamNotConst.expected | Expected results for the new test suite. |
| cpp/common/test/includes/standard-library/vector.h | Fixes cbegin/cend to be const, supporting the rule’s const-member-call logic. |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll | Registers the new Declarations6 package in query metadata plumbing. |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/Declarations6.qll | New autogenerated exclusions metadata module for the Declarations6 package. |
Copilot's findings
- Files reviewed: 10/10 changed files
- Comments generated: 10
…larations-10-1-1-properly-qualify-lvalue-refs-pointers
There was a problem hiding this comment.
Pull request overview
Adds a new MISRA C++:2023 rule implementation for RULE-10-1-1 to require const on pointer/lvalue-reference parameter target types when the target is not modified, along with supporting metadata and tests.
Changes:
- Added new query
PointerOrRefParamNotConstforRULE-10-1-1, plus unit tests and expected results. - Introduced a new rule package
Declarations6and wired it into the exclusions/metadata infrastructure andrules.csv. - Added a reusable
CallArgumentExprhelper and corrected the test standard-libraryvectormock to makecbegin/cendconst.
Show a summary per file
| File | Description |
|---|---|
| rules.csv | Routes RULE-10-1-1 to the new Declarations6 package. |
| rule_packages/cpp/Declarations6.json | Adds rule-package metadata for the new RULE-10-1-1 query. |
| cpp/misra/src/rules/RULE-10-1-1/PointerOrRefParamNotConst.ql | New query implementation for detecting missing const on pointee/referent types. |
| cpp/misra/test/rules/RULE-10-1-1/test.cpp | New test corpus with COMPLIANT/NON_COMPLIANT cases for the query. |
| cpp/misra/test/rules/RULE-10-1-1/PointerOrRefParamNotConst.qlref | Test reference to the production query. |
| cpp/misra/test/rules/RULE-10-1-1/PointerOrRefParamNotConst.expected | Expected results for the new test suite. |
| cpp/common/test/includes/standard-library/vector.h | Fixes cbegin/cend signatures to be const for tests. |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll | Registers the new Declarations6 package in query metadata wiring. |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/Declarations6.qll | Adds autogenerated exclusions package module for RULE-10-1-1 query. |
| cpp/common/src/codingstandards/cpp/Call.qll | Adds CallArgumentExpr helper to abstract call-argument/parameter-type lookups. |
Copilot's findings
Comments suppressed due to low confidence (2)
cpp/common/src/codingstandards/cpp/Call.qll:48
- The QLDoc for
getParamType()uses "its" where "it is" is intended (and the project QLDoc guidelines avoid contractions). Please rephrase to "whether it is based on...".
/**
* Gets the `Type` of the parameter corresponding to this argument, whether its based on the
* target function or the function pointer type.
*/
Type getParamType() { result = paramType }
cpp/misra/test/rules/RULE-10-1-1/test.cpp:180
p6 /= 0;introduces an unconditional division by zero in the test source. Even though it is test-only, it is still undefined behavior and can cause compilation warnings or toolchain-specific issues. Please use a non-zero divisor while still exercising the intended side effect.
p4 += 0;
*p5 *= 0;
p6 /= 0;
p7++;
if (p8) {
- Files reviewed: 10/10 changed files
- Comments generated: 3
| /** | ||
| * An `Expr` that is used as an argument to a `Call`, and has helpers to handle with the differences | ||
| * between `ExprCall` and `FunctionCall` cases. | ||
| */ |
There was a problem hiding this comment.
The QLDoc for CallArgumentExpr contains awkward/incorrect phrasing ("helpers to handle with the differences"). Please reword this sentence to be grammatically correct and unambiguous.
This issue also appears on line 44 of the same file.
| /** | ||
| * Holds if the function is in a template scope and should be excluded. | ||
| */ | ||
| predicate isInTemplateScope(Function f) { | ||
| f.isFromTemplateInstantiation(_) | ||
| or | ||
| f.isFromUninstantiatedTemplate(_) | ||
| } |
There was a problem hiding this comment.
The QLDoc for isInTemplateScope should explicitly mention its parameter f (the project's QLDoc style requires documenting predicate parameters). Consider rephrasing to "Holds if f is in a template scope and should be excluded."
Description
Require const where possible.
Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
RULE-10-1-1Release 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.