Skip to content

Rule 0-1-1, unnecessary assignments.#1021

Open
MichaelRFairhurst wants to merge 10 commits intomainfrom
michaelrfairhurst/deadcode-5-rule-0-1-1
Open

Rule 0-1-1, unnecessary assignments.#1021
MichaelRFairhurst wants to merge 10 commits intomainfrom
michaelrfairhurst/deadcode-5-rule-0-1-1

Conversation

@MichaelRFairhurst
Copy link
Collaborator

Description

Implement Rule-0-1-1, unnecessary assignments.

Copied the c Objects and SubObjects libraries and adapted them as needed for this C++ query.

Added BoundedIndex class to track potentially overlapping index accesses on arrays and STL containers.

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

Copilot AI review requested due to automatic review settings January 30, 2026 17:07
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 implements MISRA C++ Rule 0-1-1, which detects unnecessary writes to local objects. The implementation introduces a comprehensive data flow analysis framework to track writes to objects with automatic storage duration and identify cases where those writes are never observed or are overwritten before being read.

Changes:

  • Implements Rule 0-1-1 query with sophisticated object tracking and control flow analysis to detect unnecessary writes
  • Adds new C++ library modules for object lifetime analysis (CppObjects.qll, CppSubObjects.qll, StorageDuration.qll)
  • Creates STL container modeling library (STLContainers.qll) with index-based tracking via BoundedIndex class
  • Refactors existing Iterators.qll to extract STL container functionality into a dedicated module
  • Adds SuccessorUnless.qll module for control flow graph traversal
  • Includes comprehensive test suite with 749 lines covering various edge cases

Reviewed changes

Copilot reviewed 21 out of 22 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
rules.csv Updates rule metadata to assign Rule 0-1-1 to DeadCode5 package
rule_packages/cpp/DeadCode5.json Defines new DeadCode5 package with query metadata for Rule 0-1-1
cpp/misra/src/rules/RULE-0-1-1/UnnecessaryWriteToLocalObject.ql Main query implementing unnecessary write detection with TrackedObject and CrementAwareNode abstractions
cpp/misra/test/rules/RULE-0-1-1/test.cpp Comprehensive test file with 749 lines covering compliant and non-compliant cases
cpp/misra/test/rules/RULE-0-1-1/UnnecessaryWriteToLocalObject.qlref Test reference file
cpp/misra/test/rules/RULE-0-1-1/UnnecessaryWriteToLocalObject.expected Expected test results with 47 findings
cpp/common/src/codingstandards/cpp/lifetimes/CppObjects.qll New library for C++ object identity tracking with storage duration analysis
cpp/common/src/codingstandards/cpp/lifetimes/CppSubObjects.qll New library for tracking subobjects within C++ objects (fields, array elements)
cpp/common/src/codingstandards/cpp/lifetimes/StorageDuration.qll New library defining storage duration types (static, automatic, thread, allocated)
cpp/common/src/codingstandards/cpp/standardlibrary/STLContainers.qll New library modeling STL containers with BoundedIndex for tracking container element accesses
cpp/common/src/codingstandards/cpp/standardlibrary/Iterators.qll Refactored to import STLContainers instead of defining STLContainer inline
cpp/common/src/codingstandards/cpp/dominance/SuccessorUnless.qll New module for CFG traversal finding successors unless a condition is met
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll Adds DeadCode5 package to metadata system
cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode5.qll Auto-generated exclusion metadata for DeadCode5 package
cpp/common/src/codingstandards/cpp/Iterators.qll Removes duplicate STLContainer definition now in STLContainers.qll
cpp/common/test/includes/standard-library/utility.h Adds std::pair definition and include guards
cpp/common/test/includes/standard-library/vector.h Adds destructor declaration to std::vector
cpp/common/test/includes/standard-library/memory.h Moves std::allocator definition from string header
cpp/common/test/includes/standard-library/map Updates iterator typedefs to use std::pair value_type
cpp/common/test/includes/standard-library/algorithm Removes duplicate std::pair forward declaration
cpp/common/test/includes/standard-library/string Removes std::allocator definition (moved to memory.h)
c/common/src/codingstandards/c/Objects.qll Updates import path for StorageDuration.qll
Comments suppressed due to low confidence (3)

cpp/misra/src/rules/RULE-0-1-1/UnnecessaryWriteToLocalObject.ql:20

  • The import 'semmle.code.cpp.dataflow.internal.TaintTrackingUtil as Taint' on line 20 is imported but never used (the alias 'Taint' is not referenced). The predicate 'childExprTaintStep' is defined locally and doesn't use this import. Consider removing this unused import.
import semmle.code.cpp.dataflow.internal.TaintTrackingUtil as Taint

cpp/misra/src/rules/RULE-0-1-1/UnnecessaryWriteToLocalObject.ql:28

  • The import 'semmle.code.cpp.ir.dataflow.internal.SsaInternalsCommon as Ssa' on line 28 is imported but never used (the alias 'Ssa' is not referenced anywhere in the query). Consider removing this unused import.
import semmle.code.cpp.ir.dataflow.internal.SsaInternalsCommon as Ssa

cpp/misra/src/rules/RULE-0-1-1/UnnecessaryWriteToLocalObject.ql:29

  • The import 'semmle.code.cpp.dataflow.new.DataFlow as NewDataFlow' on line 29 is imported but never used (the alias 'NewDataFlow' is not referenced anywhere in the query). Consider removing this unused import.
import semmle.code.cpp.dataflow.new.DataFlow as NewDataFlow

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

@MichaelRFairhurst MichaelRFairhurst changed the title First draft of Rule 0-1-1, unnecessary assignments. Rule 0-1-1, unnecessary assignments. Feb 5, 2026
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