Skip to content

Conversation

@MichaelRFairhurst
Copy link
Collaborator

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

  • 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-1
  • 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!)

…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.
Copilot AI review requested due to automatic review settings December 4, 2025 04:21
@MichaelRFairhurst
Copy link
Collaborator Author

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

Copilot finished reviewing on behalf of MichaelRFairhurst December 4, 2025 04:25
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

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
Copy link

Copilot AI Dec 4, 2025

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.

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

Copilot uses AI. Check for mistakes.
* 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
Copy link

Copilot AI Dec 4, 2025

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

Suggested change
* (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

Copilot uses AI. Check for mistakes.
* 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:
Copy link

Copilot AI Dec 4, 2025

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

Suggested change
* // Examples types that are not `ResolvesTo<FooType>::Exactly` types:
* // Example types that are not `ResolvesTo<FooType>::Exactly` types:

Copilot uses AI. Check for mistakes.
* decltype(f) df; // does not match (non-const)
* typedef TF = FooType; // does not match (non-const)
*
* // Example `ResolvesTo<FooType>::Const` types:
Copy link

Copilot AI Dec 4, 2025

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.

Suggested change
* // Example `ResolvesTo<FooType>::Const` types:
* // Example `ResolvesTo<FooType>::CvConst` types:

Copilot uses AI. Check for mistakes.
* 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:
Copy link

Copilot AI Dec 4, 2025

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

Suggested change
* // Additional examples types that are not `ResolvesTo<FooType>::Const` types:
* // Additional example types that are not `ResolvesTo<FooType>::Const` types:

Copilot uses AI. Check for mistakes.
* 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:
Copy link

Copilot AI Dec 4, 2025

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

Suggested change
* // Examples types that are not `ResolvesTo<FooType>::Ref` types:
* // Example types that are not `ResolvesTo<FooType>::Ref` types:

Copilot uses AI. Check for mistakes.
*
* // 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)
Copy link

Copilot AI Dec 4, 2025

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

Suggested change
* FooType rf = f; // does not match (non-rerefence FooTypes)
* FooType rf = f; // does not match (non-reference FooTypes)

Copilot uses AI. Check for mistakes.
}

Type typeOfArgument(Expr e) {
// An xvalue may be a constructor, which has no return type. However, these xvalues as act values
Copy link

Copilot AI Dec 4, 2025

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

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

Copilot uses AI. Check for mistakes.
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.

2 participants