Skip to content

chore: prefer infallible conversions via From over as#6754

Merged
LesnyRumcajs merged 1 commit intomainfrom
cast-lossless-lint
Mar 17, 2026
Merged

chore: prefer infallible conversions via From over as#6754
LesnyRumcajs merged 1 commit intomainfrom
cast-lossless-lint

Conversation

@LesnyRumcajs
Copy link
Copy Markdown
Member

@LesnyRumcajs LesnyRumcajs commented Mar 17, 2026

Summary of changes

Changes introduced in this pull request:

  • as per my rant on Slack, introducing some lints to prevent potential Ariane-like fiascos

Reference issue to close (if applicable)

Closes

Other information and links

https://rust-lang.github.io/rust-clippy/rust-1.94.0/index.html#cast_lossless

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

Outside contributions

  • I have read and agree to the CONTRIBUTING document.
  • I have read and agree to the AI Policy document. I understand that failure to comply with the guidelines will lead to rejection of the pull request.

Summary by CodeRabbit

  • Refactor

    • Improved type safety and consistency throughout the codebase by refactoring numeric and type conversions across multiple components, including networking, storage, and RPC layers.
  • Chores

    • Strengthened code quality standards by implementing stricter linting rules to enforce better development practices and prevent type-related issues in future changes.

@LesnyRumcajs LesnyRumcajs requested a review from a team as a code owner March 17, 2026 09:24
@LesnyRumcajs LesnyRumcajs requested review from akaladarshi and hanabi1224 and removed request for a team March 17, 2026 09:24
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 88a9afbf-6232-45c1-8096-1f22f52eb0c3

📥 Commits

Reviewing files that changed from the base of the PR and between f0ac8db and d12a884.

📒 Files selected for processing (22)
  • Cargo.toml
  • src/chain_sync/sync_status.rs
  • src/chain_sync/validation.rs
  • src/db/car/forest/index/hash.rs
  • src/db/car/forest/index/mod.rs
  • src/db/car/plain.rs
  • src/fil_cns/validation.rs
  • src/libp2p/behaviour.rs
  • src/libp2p_bitswap/bitswap_pb.rs
  • src/message_pool/block_prob.rs
  • src/message_pool/msgpool/msg_pool.rs
  • src/networks/metrics.rs
  • src/networks/mod.rs
  • src/rpc/auth_layer.rs
  • src/rpc/filter_layer.rs
  • src/rpc/methods/eth.rs
  • src/rpc/methods/gas.rs
  • src/rpc/methods/state.rs
  • src/shim/gas.rs
  • src/state_manager/mod.rs
  • src/tool/subcommands/api_cmd/api_compare_tests.rs
  • src/tool/subcommands/snapshot_cmd.rs

Walkthrough

This PR adds a new Clippy lint rule enforcing explicit type conversions and applies it across the codebase by replacing type casts using the as operator with explicit From trait conversions (e.g., value as u64 becomes u64::from(value)).

Changes

Cohort / File(s) Summary
Clippy Configuration
Cargo.toml
Adds cast_lossless = "deny" lint setting to workspace lints.
Chain Sync and Validation
src/chain_sync/sync_status.rs, src/chain_sync/validation.rs, src/fil_cns/validation.rs
Replaces direct as u64 casts of block/epoch delay values with u64::from() conversions for type safety.
Database and Car Format
src/db/car/forest/index/hash.rs, src/db/car/forest/index/mod.rs, src/db/car/plain.rs
Replaces as u128 and as u64 casts with explicit u128::from() and u64::from() conversions in data access logic.
Network and P2P
src/networks/metrics.rs, src/networks/mod.rs, src/libp2p/behaviour.rs
Replaces numeric casts for network configuration and metric encoding with explicit From conversions; updates block delay arithmetic.
RPC Layer
src/rpc/auth_layer.rs, src/rpc/filter_layer.rs, src/rpc/methods/eth.rs, src/rpc/methods/gas.rs, src/rpc/methods/state.rs
Replaces HTTP status code, boolean, and numeric casts with explicit From conversions in RPC response construction.
Protocol Buffers
src/libp2p_bitswap/bitswap_pb.rs
Replaces boolean-to-u64 casts in protobuf message size calculations with u64::from() conversions.
Message Pool
src/message_pool/block_prob.rs, src/message_pool/msgpool/msg_pool.rs
Replaces numeric casts in probability calculations and interval computation with explicit From conversions.
State and Gas Management
src/state_manager/mod.rs, src/shim/gas.rs
Replaces u64::MAX as u128 casts with u128::from(u64::MAX) in duration clamping operations.
Testing Tools
src/tool/subcommands/api_cmd/api_compare_tests.rs, src/tool/subcommands/snapshot_cmd.rs
Replaces numeric casts in test parameter construction and epoch calculations with explicit From conversions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • hanabi1224
  • akaladarshi
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 39.39% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main objective: introducing lints to prefer infallible From trait conversions over as casts throughout the codebase.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cast-lossless-lint
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch cast-lossless-lint
📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

@LesnyRumcajs LesnyRumcajs enabled auto-merge March 17, 2026 09:24
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 17, 2026

Codecov Report

❌ Patch coverage is 69.69697% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.66%. Comparing base (f0ac8db) to head (d12a884).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/libp2p_bitswap/bitswap_pb.rs 0.00% 0 Missing and 3 partials ⚠️
src/tool/subcommands/snapshot_cmd.rs 0.00% 3 Missing ⚠️
src/chain_sync/sync_status.rs 0.00% 1 Missing ⚠️
src/networks/metrics.rs 0.00% 1 Missing ⚠️
src/rpc/auth_layer.rs 0.00% 1 Missing ⚠️
src/rpc/filter_layer.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
src/chain_sync/validation.rs 78.72% <100.00%> (ø)
src/db/car/forest/index/hash.rs 100.00% <100.00%> (ø)
src/db/car/forest/index/mod.rs 87.76% <100.00%> (ø)
src/db/car/plain.rs 82.71% <100.00%> (ø)
src/fil_cns/validation.rs 78.75% <100.00%> (+0.06%) ⬆️
src/libp2p/behaviour.rs 68.25% <100.00%> (ø)
src/message_pool/block_prob.rs 87.64% <100.00%> (ø)
src/message_pool/msgpool/msg_pool.rs 78.08% <100.00%> (+0.21%) ⬆️
src/networks/mod.rs 88.91% <100.00%> (ø)
src/rpc/methods/eth.rs 68.12% <100.00%> (ø)
... and 10 more

... and 6 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f0ac8db...d12a884. Read the comment docs.

@LesnyRumcajs LesnyRumcajs added this pull request to the merge queue Mar 17, 2026
Merged via the queue into main with commit ef963b4 Mar 17, 2026
34 of 35 checks passed
@LesnyRumcajs LesnyRumcajs deleted the cast-lossless-lint branch March 17, 2026 10:32
@coderabbitai coderabbitai Bot mentioned this pull request Apr 9, 2026
6 tasks
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.

2 participants