Rule 0-1-1, unnecessary assignments.#1021
Conversation
There was a problem hiding this comment.
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.
cpp/common/src/codingstandards/cpp/standardlibrary/STLContainers.qll
Outdated
Show resolved
Hide resolved
cpp/common/src/codingstandards/cpp/standardlibrary/STLContainers.qll
Outdated
Show resolved
Hide resolved
cpp/misra/src/rules/RULE-0-1-1/UnnecessaryWriteToLocalObject.ql
Outdated
Show resolved
Hide resolved
cpp/misra/src/rules/RULE-0-1-1/UnnecessaryWriteToLocalObject.ql
Outdated
Show resolved
Hide resolved
cpp/common/src/codingstandards/cpp/standardlibrary/STLContainers.qll
Outdated
Show resolved
Hide resolved
cpp/common/src/codingstandards/cpp/standardlibrary/STLContainers.qll
Outdated
Show resolved
Hide resolved
…dcode-5-rule-0-1-1
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
BoundedIndexclass to track potentially overlapping index accesses on arrays and STL containers.Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
RULE-0-1-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.