Skip to content

Michaelrfairhurst/package deadcode 9 rule 0-2-3#1043

Open
MichaelRFairhurst wants to merge 4 commits intomainfrom
michaelrfairhurst/package-deadcode-9-rule-0-2-3
Open

Michaelrfairhurst/package deadcode 9 rule 0-2-3#1043
MichaelRFairhurst wants to merge 4 commits intomainfrom
michaelrfairhurst/package-deadcode-9-rule-0-2-3

Conversation

@MichaelRFairhurst
Copy link
Collaborator

@MichaelRFairhurst MichaelRFairhurst commented Feb 18, 2026

Description

Updates to find type uses

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 0-2-31
  • Queries have been modified for the following rules:
    • RULE 2-3
    • A0-1-6

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 18:08
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 pull request adds support for MISRA C++ 2023 RULE-0-2-3, which identifies types with limited visibility (local types or types in anonymous namespaces) that are not used. Additionally, it improves the type usage analysis shared by multiple rules (RULE-2-3 for C, A0-1-6 for C++ AUTOSAR, and the new RULE-0-2-3 for C++ MISRA) to more accurately identify type uses and exclude self-referential uses.

Changes:

  • Added RULE-0-2-3 query implementation for detecting unused types with limited visibility
  • Introduced new DeadCode9 package to categorize this rule
  • Enhanced type usage analysis to better handle anonymous types, template specializations, hidden friends, and nested declarations

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
rules.csv Updated RULE-0-2-3 entry to use DeadCode9 package
rule_packages/cpp/DeadCode9.json New rule package metadata for RULE-0-2-3
cpp/misra/test/rules/RULE-0-2-3/test.cpp Comprehensive test cases for RULE-0-2-3 covering various edge cases
cpp/misra/test/rules/RULE-0-2-3/UnusedTypeWithLimitedVisibility.qlref Test reference file for the query
cpp/misra/test/rules/RULE-0-2-3/UnusedTypeWithLimitedVisibility.expected Expected test results
cpp/misra/src/rules/RULE-0-2-3/UnusedTypeWithLimitedVisibility.ql Main query implementation for RULE-0-2-3
cpp/common/test/rules/unusedtypedeclarations/test.cpp Additional test cases for nested types
cpp/common/test/rules/unusedtypedeclarations/UnusedTypeDeclarations.expected Updated expected results
cpp/common/src/codingstandards/cpp/types/Uses.qll Enhanced type usage analysis with isWithinTypeDefinition predicate to exclude self-referential uses
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll Added DeadCode9 package integration
cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode9.qll New exclusion module for DeadCode9 package
cpp/common/src/codingstandards/cpp/ast/HiddenFriend.qll New module to identify hidden friend functions (uses Class.qll)
cpp/common/src/codingstandards/cpp/ast/Class.qll New utility module for identifying the last declaration in a class
change_notes/2026-02-18-unused-type-improvements.md Documents improvements to type usage analysis
c/common/test/rules/unusedtypedeclarations/test.c Additional test cases for C typedef handling
c/common/test/rules/unusedtypedeclarations/UnusedTypeDeclarations.expected Updated expected results for C tests
Comments suppressed due to low confidence (2)

cpp/common/src/codingstandards/cpp/ast/HiddenFriend.qll:66

  • The code calls 'last.getDeclaringType()' where 'last' is of type LastClassDeclaration, but the LastClassDeclaration class in Class.qll does not define a getDeclaringType() method. This will cause a compilation error. A getDeclaringType() method needs to be added to LastClassDeclaration that returns the 'cls' field.
      last.getDeclaringType() = this and

cpp/common/src/codingstandards/cpp/ast/HiddenFriend.qll:81

  • The code calls 'last.getDeclaringType()' where 'last' is of type LastClassDeclaration, but the LastClassDeclaration class in Class.qll does not define a getDeclaringType() method. This will cause a compilation error. A getDeclaringType() method needs to be added to LastClassDeclaration that returns the 'cls' field.
      last.getDeclaringType() = this and

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

namespace {
struct A1 {
A1 f();
}; // COMPLIANT - A1 is used below
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 test case comment states 'COMPLIANT - A1 is used below' referring to line 36 where 'A1 l1;' is declared. However, line 36 is inside an uninstantiated template function 'foo()'. Since the template is never instantiated, A1 is not actually used, and the expected result correctly identifies it as NON_COMPLIANT. The comment should be updated to 'NON_COMPLIANT - A1 is only referenced in an uninstantiated template' to match the expected behavior.

Suggested change
}; // COMPLIANT - A1 is used below
}; // NON_COMPLIANT - A1 is only referenced in an uninstantiated template

Copilot uses AI. Check for mistakes.
Comment on lines +92 to +100
//exists(TemplateClass tpl, ClassTemplateSpecialization spec |
// tpl.getAnInstantiation() = type and
// tpl = spec.getPrimaryTemplate() and
// isWithinTypeDefinition(loc, spec)
//)
//exists(ClassTemplateSpecialization spec |
// specializationSharesType(type, spec) and
// isWithinTypeDefinition(loc, spec)
//)
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.

There is commented-out code in the isWithinTypeDefinition predicate (lines 92-100). If this code is no longer needed, it should be removed. If it's being kept for future reference or debugging, a comment should be added explaining why it's commented out and whether it will be used in the future.

Suggested change
//exists(TemplateClass tpl, ClassTemplateSpecialization spec |
// tpl.getAnInstantiation() = type and
// tpl = spec.getPrimaryTemplate() and
// isWithinTypeDefinition(loc, spec)
//)
//exists(ClassTemplateSpecialization spec |
// specializationSharesType(type, spec) and
// isWithinTypeDefinition(loc, spec)
//)

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

/test-performance

@github-actions
Copy link

🏁 Beep Boop! Performance testing for this PR has been initiated. Please check back later for results. Note that the query package generation step must complete before testing will start so it might be a minute.

💡 If you do not hear back from me please check my status! I will report even if I fail!

@codeql-coding-standards-automation

🏁 Beep Boop! Performance testing complete! See below for performance of the last 3 runs vs your PR. Times are based on predicate performance. You can find full graphs and stats in the PR that was created for this test in the release engineering repo.


Release                            : v2.53.0
Platform                           : x86-windows
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2854906
Mean_Predicate_Execution_Time_Ms   : 61.82528098403967
Median_Predicate_Execution_Time_Ms : 1.0
Standard_Deviation_Ms              : 415.5458433646134
Total_Serialized_Execution_Time_s  : 2854.906
Mean_Query_Execution_Time_s        : 0.0618252809840396
Median_Predicate_Execution_Time_s  : 0.001
Percentile95_Ms                    : 197.0
Number_of_Predicates               : 46177

Release                            : v2.53.0
Platform                           : x86-windows
Language                           : c
Total_Serialized_Execution_Time_Ms : 2749208
Mean_Predicate_Execution_Time_Ms   : 55.15846073592552
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 416.7448594682875
Total_Serialized_Execution_Time_s  : 2749.208
Mean_Query_Execution_Time_s        : 0.0551584607359255
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 178.9499999999971
Number_of_Predicates               : 49842

Release                            : v2.53.0
Platform                           : x86-linux
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2709046
Mean_Predicate_Execution_Time_Ms   : 58.63989783107494
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 538.09393160794
Total_Serialized_Execution_Time_s  : 2709.046
Mean_Query_Execution_Time_s        : 0.0586398978310749
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 157.0
Number_of_Predicates               : 46198

Release                            : v2.53.0
Platform                           : x86-linux
Language                           : c
Total_Serialized_Execution_Time_Ms : 2333626
Mean_Predicate_Execution_Time_Ms   : 46.86466512702079
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 464.3841141059346
Total_Serialized_Execution_Time_s  : 2333.626
Mean_Query_Execution_Time_s        : 0.0468646651270207
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 129.0
Number_of_Predicates               : 49795

Release                            : v2.54.0
Platform                           : x86-windows
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 3096680
Mean_Predicate_Execution_Time_Ms   : 66.35979856423444
Median_Predicate_Execution_Time_Ms : 1.0
Standard_Deviation_Ms              : 423.5921363539812
Total_Serialized_Execution_Time_s  : 3096.68
Mean_Query_Execution_Time_s        : 0.0663597985642344
Median_Predicate_Execution_Time_s  : 0.001
Percentile95_Ms                    : 203.0
Number_of_Predicates               : 46665

Release                            : v2.54.0
Platform                           : x86-windows
Language                           : c
Total_Serialized_Execution_Time_Ms : 2926320
Mean_Predicate_Execution_Time_Ms   : 57.99171637502229
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 461.2110774874936
Total_Serialized_Execution_Time_s  : 2926.32
Mean_Query_Execution_Time_s        : 0.0579917163750222
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 180.0
Number_of_Predicates               : 50461

Release                            : v2.54.0
Platform                           : x86-linux
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2756233
Mean_Predicate_Execution_Time_Ms   : 59.12125697125697
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 526.333362805237
Total_Serialized_Execution_Time_s  : 2756.233
Mean_Query_Execution_Time_s        : 0.0591212569712569
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 157.0
Number_of_Predicates               : 46620

Release                            : v2.54.0
Platform                           : x86-linux
Language                           : c
Total_Serialized_Execution_Time_Ms : 2430395
Mean_Predicate_Execution_Time_Ms   : 48.376659567268455
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 494.4557213724258
Total_Serialized_Execution_Time_s  : 2430.395
Mean_Query_Execution_Time_s        : 0.0483766595672684
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 125.09999999999854
Number_of_Predicates               : 50239

Release                            : 1043
Platform                           : x86-linux
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2966860
Mean_Predicate_Execution_Time_Ms   : 63.34571696985225
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 576.394593085505
Total_Serialized_Execution_Time_s  : 2966.86
Mean_Query_Execution_Time_s        : 0.0633457169698522
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 164.0
Number_of_Predicates               : 46836

Release                            : 1043
Platform                           : x86-linux
Language                           : c
Total_Serialized_Execution_Time_Ms : 2468112
Mean_Predicate_Execution_Time_Ms   : 48.89286846275753
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 507.27843761931456
Total_Serialized_Execution_Time_s  : 2468.112
Mean_Query_Execution_Time_s        : 0.0488928684627575
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 126.0
Number_of_Predicates               : 50480

🏁 Below are the slowest predicates for the last 2 releases vs this PR.


Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CheckedException::CheckedException#b0aa5ec8
Execution_Time_Ms : 26887

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : Dependency::dependsOnTransitive/2#cbda84a0
Execution_Time_Ms : 26305

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : #select
Execution_Time_Ms : 22092

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTableSimpleString/5#6de8614f#cpe#1236
Execution_Time_Ms : 33193

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : Macro::MacroInvocation.getAnAffectedElement/0#dispred#d1462297_10#join_rhs
Execution_Time_Ms : 31405

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : _Class::Class.getALinkTarget/0#dispred#29b2b38a#bf_Class::Class.getALinkTarget/0#dispred#29b2b38a#bf__#shared
Execution_Time_Ms : 26026

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : Macro::MacroInvocation.getAnAffectedElement/0#dispred#d1462297_10#join_rhs
Execution_Time_Ms : 33782

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTable/5#79217c12
Execution_Time_Ms : 21202

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTableSimpleString/5#6de8614f#cpe#1236
Execution_Time_Ms : 30138

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : _Macro::Macro.getName/0#dispred#e28b3699_Preprocessor::PreprocessorBranchDirective#bcd2bde4#b_Prepro__#antijoin_rhs
Execution_Time_Ms : 27240

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 16113

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : FunctionEquivalence::typeSig/1#194ac728#bf
Execution_Time_Ms : 14027

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : FunctionEquivalence::typeSig/1#194ac728
Execution_Time_Ms : 26442

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : CharacterOutsideTheLanguageStandardBasicSourceCharacterSetUsedInTheSourceCode::getUniversalCharacterName/1#36dbaa42
Execution_Time_Ms : 15807

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : Dependency::dependsOnTransitive/2#cbda84a0
Execution_Time_Ms : 19625

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 25179

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Macro::Macro.getName/0#dispred#e28b3699_Preprocessor::PreprocessorBranchDirective#bcd2bde4#b_Prepro__#antijoin_rhs
Execution_Time_Ms : 28686

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Class::Class.getALinkTarget/0#dispred#29b2b38a#bf_Class::Class.getALinkTarget/0#dispred#29b2b38a#bf__#shared
Execution_Time_Ms : 24575

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CharacterOutsideTheLanguageStandardBasicSourceCharacterSetUsedInTheSourceCode::getUniversalCharacterName/1#36dbaa42
Execution_Time_Ms : 32616

Release           : v2.54.0
Run               : 2026-01-27_17-28-56
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 28849

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : HardwareOrProtocolInterface::HardwareOrProtocolInterfaceComment#dbbd8a01
Execution_Time_Ms : 23538

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CheckedException::CheckedException#b0aa5ec8
Execution_Time_Ms : 28152

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 30762

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CharacterOutsideTheLanguageStandardBasicSourceCharacterSetUsedInTheSourceCode::getUniversalCharacterName/1#36dbaa42
Execution_Time_Ms : 34691

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : VirtualDispatchPrototype::VirtualDispatch::cannotInheritHelper/4#7c75bd87
Execution_Time_Ms : 25028

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTableSimpleString/5#6de8614f#cpe#1236
Execution_Time_Ms : 34799

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Macro::Macro.getName/0#dispred#e28b3699_Preprocessor::PreprocessorBranchDirective#bcd2bde4#b_Prepro__#antijoin_rhs
Execution_Time_Ms : 30178

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Class::Class.getALinkTarget/0#dispred#29b2b38a#bf_Class::Class.getALinkTarget/0#dispred#29b2b38a#bf__#shared
Execution_Time_Ms : 25516

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : Macro::MacroInvocation.getAnAffectedElement/0#dispred#d1462297_10#join_rhs
Execution_Time_Ms : 31470

Release           : 1043
Run               : 2026-02-18_20-36-41
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 25669

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