Michaelrfairhurst/declarations8 rule 6-2-3 do not duplicate source code#1112
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds new MISRA C++:2023 RULE-6-2-3 queries (and their tests) to detect duplicate entity implementations/types across files and misplaced template specializations, while also refactoring shared anonymous-namespace linkage logic used by multiple queries.
Changes:
- Add a new C++ rule package entry for
RULE-6-2-3and three associated query definitions. - Add unit tests (
.cpp/.h) and.expected/.qlreffiles covering duplicate type definitions and template specialization placement. - Refactor
Linkage.qllanonymous-namespace detection (introducingWithinAnonymousNamespace) and wire the new package into the exclusions metadata, with a change note.
Show a summary per file
| File | Description |
|---|---|
| rule_packages/cpp/Declarations8.json | Registers RULE-6-2-3 and its three queries in the rule package metadata. |
| cpp/misra/src/rules/RULE-6-2-3/SourceCodeImplementedOnlyOnce.ql | New query intended to detect multiple implementations of an entity across source locations. |
| cpp/misra/src/rules/RULE-6-2-3/TemplateSpecializationWrongLocation.ql | New query to flag template specializations declared outside the primary template / specialized-type file. |
| cpp/misra/src/rules/RULE-6-2-3/DuplicateTypeDefinitions.ql | New query to flag duplicate type definitions across files. |
| cpp/misra/test/rules/RULE-6-2-3/test.cpp | Primary test file containing compliant/non-compliant cases and includes for specialization tests. |
| cpp/misra/test/rules/RULE-6-2-3/test2.cpp | Secondary TU to exercise cross-file duplication behavior. |
| cpp/misra/test/rules/RULE-6-2-3/template.h | Defines primary templates and compliant specializations for location tests. |
| cpp/misra/test/rules/RULE-6-2-3/class.h | Defines types used as specialization arguments for location tests. |
| cpp/misra/test/rules/RULE-6-2-3/compliant_specialization.h | Compliant specialization cases (specialized type defined in same file). |
| cpp/misra/test/rules/RULE-6-2-3/noncompliant_specialization.h | Non-compliant specialization cases expected to be flagged. |
| cpp/misra/test/rules/RULE-6-2-3/SourceCodeImplementedOnlyOnce.qlref | Test reference to the production query. |
| cpp/misra/test/rules/RULE-6-2-3/SourceCodeImplementedOnlyOnce.expected | Expected results for the inline-entity duplication query. |
| cpp/misra/test/rules/RULE-6-2-3/TemplateSpecializationWrongLocation.qlref | Test reference to the production query. |
| cpp/misra/test/rules/RULE-6-2-3/TemplateSpecializationWrongLocation.expected | Expected results for the specialization-location query. |
| cpp/misra/test/rules/RULE-6-2-3/DuplicateTypeDefinitions.qlref | Test reference to the production query. |
| cpp/misra/test/rules/RULE-6-2-3/DuplicateTypeDefinitions.expected | Expected results for the duplicate-type-definition query. |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll | Wires the new Declarations8 package into the exclusions metadata dispatch. |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/Declarations8.qll | Adds autogenerated exclusions metadata and query handles for Declarations8. |
| cpp/common/src/codingstandards/cpp/Linkage.qll | Refactors nested anonymous-namespace detection via WithinAnonymousNamespace. |
| change_notes/2026-04-19-refactor-nested-anonymous-namespace-logic.md | Change note covering the Linkage refactor impact statement. |
Copilot's findings
Comments suppressed due to low confidence (1)
cpp/misra/src/rules/RULE-6-2-3/SourceCodeImplementedOnlyOnce.ql:39
- This query only checks
Function.isInline(), but the alert message says "Inline variable". That’s misleading for users (and contradicts the stated implementation scope). Update the message (and any related wording) to refer to an inline function (or more generally an inline entity) rather than an inline variable.
select d1,
"Inline variable '" + d1.getName() +
"' is defined in multiple files, violating the source code uniqueness requirement."
- Files reviewed: 20/20 changed files
- Comments generated: 3
| @@ -0,0 +1,40 @@ | |||
| /** | |||
| * @id cpp/misra/template-specialization-wrong-location | |||
| * @name RULE-6-2-3: RULE-6-2-3: Template specializations in wrong location | |||
There was a problem hiding this comment.
The query metadata @name field includes a duplicated rule prefix ("RULE-6-2-3: RULE-6-2-3: …"). This will show up in tooling/UI and is inconsistent with the rule package name; please remove the duplicated "RULE-6-2-3:" segment.
| * @name RULE-6-2-3: RULE-6-2-3: Template specializations in wrong location | |
| * @name RULE-6-2-3: Template specializations in wrong location |
| @@ -0,0 +1,41 @@ | |||
| /** | |||
| * @id cpp/misra/duplicate-type-definitions | |||
| * @name RULE-6-2-3: RULE-6-2-3: Duplicate type definitions across files | |||
There was a problem hiding this comment.
The query metadata @name field includes a duplicated rule prefix ("RULE-6-2-3: RULE-6-2-3: …"). This will show up in tooling/UI and is inconsistent with the rule package name; please remove the duplicated "RULE-6-2-3:" segment.
| * @name RULE-6-2-3: RULE-6-2-3: Duplicate type definitions across files | |
| * @name RULE-6-2-3: Duplicate type definitions across files |
| from DeclarationEntry d1, DeclarationEntry d2, string namespace, string name | ||
| where | ||
| not isExcluded([d1, d2], Declarations8Package::sourceCodeImplementedOnlyOnceQuery()) and | ||
| d1 != d2 and | ||
| d1.isDefinition() and | ||
| d2.isDefinition() and | ||
| isInline(d1) and | ||
| isInline(d2) and | ||
| d1.getDeclaration().hasQualifiedName(namespace, name) and | ||
| d2.getDeclaration().hasQualifiedName(namespace, name) and | ||
| d1.getFile() != d2.getFile() and |
There was a problem hiding this comment.
hasQualifiedName(namespace, name) only compares the qualified name (not the full signature), so this can incorrectly report overloads (or other distinct entities) that share the same name across files. To ensure you only match the same entity, compare the underlying declarations directly (for example d1.getDeclaration() = d2.getDeclaration()), and then you can drop the extra namespace/name string bindings.
This issue also appears on line 37 of the same file.
Description
This rule is somewhat interesting because 6-2-1 already mostly does things that 6-2-3 does. We can't expressly detect ODR violations, so 6-2-1 looks for repeated names across files. However, there's still room to make 6-2-1 stricter and that's what this query does. Essentially, 6-2-3 gives us permission to flag cases that we considered likely false positives in 6-2-1.
Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
RULE-6-2-3Release 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.