Skip to content

F-015: refactor(services): document SAFETY invariants for cast truncation#18

Merged
Sephyi merged 1 commit intodevelopmentfrom
audit/f-015-safety-comments-truncation-casts
Apr 22, 2026
Merged

F-015: refactor(services): document SAFETY invariants for cast truncation#18
Sephyi merged 1 commit intodevelopmentfrom
audit/f-015-safety-comments-truncation-casts

Conversation

@Sephyi
Copy link
Copy Markdown
Owner

@Sephyi Sephyi commented Apr 22, 2026

Summary

refactor(services): document SAFETY invariants for cast truncation.

Audit context

Closes audit entry F-015 from #3.

Verification

  • cargo fmt --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test --all-targets

Note: one pre-existing test porcelain_exits_within_timeout_with_no_staged_changes is a known macOS cold-start flake that reproduces on unmodified development — unrelated to this change.

…cast truncation

Add a one-line `// SAFETY:` comment immediately above each
`#[allow(clippy::cast_possible_truncation)]` in analyzer.rs and
differ.rs. Every annotated site performs an `i as u32` cast on a
`usize` loop index to call `tree_sitter::Node::child(u32)`. The cast
is sound because:

1. tree-sitter nodes cannot have more than `u32::MAX` children — the
   upstream API (`child_count() -> usize`) is bounded by an
   internally-stored `u32` count.
2. Each cast site's index originates from a loop bounded by
   `child_count()`, so the value is already `<= u32::MAX` by
   construction and truncation cannot lose information.

All 16 sites use identical wording for easy grepping and future
audit. No behavioural change.

Closes audit entry F-015 from #3.
Copilot AI review requested due to automatic review settings April 22, 2026 19:50
@Sephyi Sephyi added the audit Codebase audit cleanup (issue #3) label Apr 22, 2026
@Sephyi Sephyi self-assigned this Apr 22, 2026
Copy link
Copy Markdown

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

Documents the invariants behind #[allow(clippy::cast_possible_truncation)] in tree-sitter child indexing sites, addressing audit finding F-015 by making the truncation rationale explicit.

Changes:

  • Added // SAFETY: explanations adjacent to usize -> u32 casts when iterating tree-sitter node children in AstDiffer.
  • Added matching // SAFETY: explanations for the same cast pattern in AnalyzerService.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/services/differ.rs Adds SAFETY invariants next to clippy truncation allowances for tree-sitter child iteration.
src/services/analyzer.rs Adds SAFETY invariants next to clippy truncation allowances for tree-sitter child iteration.

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

@Sephyi Sephyi merged commit 734f404 into development Apr 22, 2026
11 of 13 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

audit Codebase audit cleanup (issue #3)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants