-
Notifications
You must be signed in to change notification settings - Fork 69
Implement rule 28-6-1, only move non-const lvalues #992
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
base: main
Are you sure you want to change the base?
Implement rule 28-6-1, only move non-const lvalues #992
Conversation
…ing types.
Expands upon `PossiblySpecified<T>` module, which was nothing but a renaming of
`.stripSpeciifers` that was intended to be clearer (and not resolve typedefs).
However, there is almost never a reason not to resolve _certain_ typedefs.
Assume we are writing a query to detect usages of a typedef such as `uint8_t`.
If we have a candidate type and wish to check if it is a `uint8_t`, we may do so
via `candidate.(TypedefType).hasName("uint8_t")`. However, this will not match
`const uint8_t`. The type methods such as `stripSpecifiers`,
`getUnderlyingType`, can be used but are sometimes vague, and some implicitly
resolve typedefs while others do not, and some even have incorrect
documentation.
Furthermore, if a user has declared `typedef uint8_t uchar_t` or
`typedef const uint8_t uint8_const_t`, then we _must_ resolve typedefs to find
these usages of `uint8_t` in the project. However, `candidate.resolveTypedefs()`
will also unwrap the `uint8_t`. In C++ the problem is worse too, as we would
need to detect cases such as `const uint8_t&`, and decltypes.
This project needs, IMO, a better API for incrementally resolving typedefs,
decltypes, specifiers, and references. This PR adds what is hopefully a decent
starting point for this.
Instead of writing `candidate.resolveTypedefs() instanceof FooType` or
`candidate.stripTopLevelSpecifiers() instanceof FooType` or
`candidate.getUnderlyingType() instanceof FooType`, the `cpp.types.Resolve`
import now allows `candidate instanceof ResolvesTo<FooType>::Exactly` to only
resolve typedefs and decltypes, or `::IgnoringSpecifiers` to resolve typedefs,
decltypes, and unwrap `SpecifiedType`s as well. These types have a `.resolve()`
member predicate to unwrap everything and get the resolved `FooType`.
The API basically intends to define an interterface for how to unwrap type A in
search of a type B. Types are like linked lists, with flags on certain links.
We really just want a means of traversing different kinds of links in different
contexts, sometimes requiring the presence of a certain kind of link, and
sometimes aggregating properties about what links were traversed.
We also want an API that provides some guard rails and funnels people towards
correct usage of itself. Hopefully `::Exactly` comes across as too strict, and
hopefully `CvConst` makes it clear that a type may be `const` or
`const volatile`.
Also added some types that compliment this API well such as
`PointerTo<T>::Type` and `ReferenceOf<T>::Type`.
Also integrated this library into a few example queries so the PR includes
example usage.
|
This PR is basically done and ready to be reviewed. However, this seems like it should be part of coding-standards-cpp-baseline. It has very few findings, and the findings can be fairly serious (moving a const lvalue is definitely an alarming finding). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements MISRA C++ 2023 rule 28-6-1, which requires that std::move only be called with non-const lvalue arguments. Calling std::move on const lvalues will not result in a move operation, and calling it on rvalues is redundant. The implementation includes a new comprehensive type resolution infrastructure that replaces the previous PossiblySpecified module with a more robust ResolvesTo system that better handles typedefs, decltypes, and CV-qualifiers. Several existing queries have been refactored to use the new type resolution modules.
- Added new query for RULE-28-6-1 with comprehensive test coverage including template instantiations
- Introduced new type resolution modules (
Resolve.qll,Specifiers.qll) and value category handling (ValueCategory.qll) - Refactored 4 existing queries to use the new type resolution infrastructure
Reviewed changes
Copilot reviewed 36 out of 37 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
rules.csv |
Added RULE-28-6-1 mapping to Preconditions5 package |
rule_packages/cpp/Preconditions5.json |
New rule package metadata for RULE-28-6-1 |
cpp/misra/test/rules/RULE-28-6-1/test.cpp |
Comprehensive test cases covering const/non-const lvalues, rvalues, and template scenarios |
cpp/misra/test/rules/RULE-28-6-1/StdMoveWithNonConstLvalue.expected |
Expected query results for 21 non-compliant std::move calls |
cpp/misra/test/rules/RULE-28-6-1/StdMoveWithNonConstLvalue.qlref |
Query reference for test execution |
cpp/misra/src/rules/RULE-28-6-1/StdMoveWithNonConstLvalue.ql |
Query implementation detecting improper std::move usage |
cpp/common/src/codingstandards/cpp/types/Resolve.qll |
New comprehensive type resolution module supporting typedef/decltype resolution and specifier handling |
cpp/common/src/codingstandards/cpp/types/Specifiers.qll |
New module for handling const/volatile specifiers |
cpp/common/src/codingstandards/cpp/types/Type.qll |
Removed deprecated PossiblySpecified module |
cpp/common/src/codingstandards/cpp/ast/ValueCategory.qll |
New module for determining expression value categories (lvalue/prvalue/xvalue) |
cpp/misra/src/rules/RULE-9-5-1/LegacyForStatementsShouldBeSimple.ql |
Refactored to use new type resolution for pointer/reference type checking |
c/misra/src/rules/RULE-22-12/NonstandardUseOfThreadingObject.ql |
Updated to use ResolvesTo for threading object type detection |
c/misra/src/rules/RULE-22-13/ThreadingObjectWithInvalidStorageDuration.ql |
Updated to use ResolvesTo for threading object type detection |
c/misra/src/rules/RULE-22-14/MutexNotInitializedBeforeUse.ql |
Updated to use ResolvesTo for mutex/condition type detection |
c/cert/src/rules/INT36-C/ConvertingAPointerToIntegerOrIntegerToPointer.ql |
Refactored to use new type resolution for int/pointer type comparisons |
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll |
Added Preconditions5 package metadata integration |
cpp/common/src/codingstandards/cpp/exclusions/cpp/Preconditions5.qll |
Generated exclusion metadata for RULE-28-6-1 |
cpp/common/test/library/codingstandards/cpp/types/Resolve/test.cpp |
Library tests for type resolution with 42 test cases |
cpp/common/test/library/codingstandards/cpp/types/Resolve/ResolveTest.ql |
Test query for validating type resolution functionality |
change_notes/2025-12-03-type-resolution-tracking-changes.md |
Change notes documenting the type resolution refactoring impact |
Multiple codeql-pack.lock.yml files |
Added qtil 0.0.3 dependency across all packages |
cpp/common/src/qlpack.yml |
Added qtil 0.0.3 dependency declaration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /** | ||
| * A type that resolves exactly to the module's `ResolvedType` type parameter. | ||
| * | ||
| * For example, `ResolvesTo<FooType>::Type` is the set of all `FooType`s and types that resolve |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example text refers to ResolvesTo<FooType>::Type but the actual class name is Exactly, not Type. This should be corrected to ResolvesTo<FooType>::Exactly.
| * For example, `ResolvesTo<FooType>::Type` is the set of all `FooType`s and types that resolve | |
| * For example, `ResolvesTo<FooType>::Exactly` is the set of all `FooType`s and types that resolve |
| * A type that resolves exactly to the module's `ResolvedType` type parameter. | ||
| * | ||
| * For example, `ResolvesTo<FooType>::Type` is the set of all `FooType`s and types that resolve | ||
| * (through typedefs * and/or decltypes) to `FooType`s. This does _not_ include types that resolve |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a spurious asterisk in "typedefs * and/or decltypes" that should be removed. It should read "typedefs and/or decltypes".
| * (through typedefs * and/or decltypes) to `FooType`s. This does _not_ include types that resolve | |
| * (through typedefs and/or decltypes) to `FooType`s. This does _not_ include types that resolve |
| * decltype(f2); // matches (a decltype of typedef of FooType) | ||
| * typedef FT FT2; // matches (a typedef of typedef of FooType) | ||
| * | ||
| * // Examples types that are not `ResolvesTo<FooType>::Exactly` types: |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammatical error: "Examples types" should be "Example types".
| * // Examples types that are not `ResolvesTo<FooType>::Exactly` types: | |
| * // Example types that are not `ResolvesTo<FooType>::Exactly` types: |
| * decltype(f) df; // does not match (non-const) | ||
| * typedef TF = FooType; // does not match (non-const) | ||
| * | ||
| * // Example `ResolvesTo<FooType>::Const` types: |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example text refers to ResolvesTo<FooType>::Const but the actual class name is CvConst, not Const. This should be corrected to ResolvesTo<FooType>::CvConst.
| * // Example `ResolvesTo<FooType>::Const` types: | |
| * // Example `ResolvesTo<FooType>::CvConst` types: |
| * typedef const FooType CFT; // matches (a typedef of const FooType) | ||
| * const TF ctf; // matches (a const typedef of FooType) | ||
| * | ||
| * // Additional examples types that are not `ResolvesTo<FooType>::Const` types: |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammatical error: "examples types" should be "example types".
| * // Additional examples types that are not `ResolvesTo<FooType>::Const` types: | |
| * // Additional example types that are not `ResolvesTo<FooType>::Const` types: |
| * decltype(f2); // matches (a decltype of typedef of ref to FooType) | ||
| * typedef FT FT2; // matches (a typedef of typedef of ref to FooType) | ||
| * | ||
| * // Examples types that are not `ResolvesTo<FooType>::Ref` types: |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammatical error: "Examples types" should be "Example types".
| * // Examples types that are not `ResolvesTo<FooType>::Ref` types: | |
| * // Example types that are not `ResolvesTo<FooType>::Ref` types: |
| * | ||
| * // Examples types that are not `ResolvesTo<FooType>::Ref` types: | ||
| * const FooType &cf; // does not match (specified references to FooTypes) | ||
| * FooType rf = f; // does not match (non-rerefence FooTypes) |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spelling error: "non-rerefence" should be "non-reference".
| * FooType rf = f; // does not match (non-rerefence FooTypes) | |
| * FooType rf = f; // does not match (non-reference FooTypes) |
| } | ||
|
|
||
| Type typeOfArgument(Expr e) { | ||
| // An xvalue may be a constructor, which has no return type. However, these xvalues as act values |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a typo in the comment: "these xvalues as act values" should be "these xvalues act as values".
| // An xvalue may be a constructor, which has no return type. However, these xvalues as act values | |
| // An xvalue may be a constructor, which has no return type. However, these xvalues act as values |
Description
Implement 28-6-1, std::move with const will not move, and std::move with a non-lvalue is redundant.
Unfortunately,
expr.getLvalueCategory()did not work out of the box, as it generates extra rvalue for load steps that are transparent to the user, so we have to navigate lvalue to rvalue conversion etc to handle properly.isLvalue()also exists, but its an incomplete syntactic match. Further, the definition of an lvalue/xvalue/etc changes across C++ versions, which that function doesn't handle. While MISRA C++ 2023 currently requires C++17, we want our queries to work for users that don't comply with that restriction, for instance, users using the next version of MISRA C++ :)Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
RULE-28-6-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.