Skip to content

refactor: adopt Rust 1.95 cfg_select! macro at platform-conditional sites#3846

Merged
jqnatividad merged 3 commits into
masterfrom
cfg_select_macro
May 12, 2026
Merged

refactor: adopt Rust 1.95 cfg_select! macro at platform-conditional sites#3846
jqnatividad merged 3 commits into
masterfrom
cfg_select_macro

Conversation

@jqnatividad
Copy link
Copy Markdown
Collaborator

Summary

  • Convert 15 platform-conditional sites across 9 files from paired #[cfg(...)] attributes (or duplicated function defs) to std's new cfg_select! macro stabilized in Rust 1.95 (current MSRV).
  • Notable wins: reset_sigpipe and is_executable collapse from 2 fn defs to 1; the platform_factor 4-arm target_os ladder becomes a single expression; the 9 target_endian simd_json/serde_json pairs lose their #[cfg] scaffolding.
  • Net −23 lines (109 insertions, 132 deletions).

Sites converted

src/util.rs

  • reset_sigpipe — merged unix / not(unix) defs
  • is_executable — merged unix / windows defs
  • platform_factor — 4-arm target_os match
  • 3 target_endian simd_json/serde_json sites
  • Signal-status error block — refactored duplicated return Err into single cfg_select! over the message

src/cmd/

  • stats.rs — 3 target_endian sites (incl. sniff_result with let pulled outside)
  • schema.rs, pragmastat.rs, validate.rs — 1 target_endian site each
  • foreach.rs — 2 unix/windows let bindings
  • tojsonl.rscount_rows_regular vs count_rows
  • scoresql.rswhich vs where
  • prompt.rs — macOS-only set_can_create_directories

One site intentionally kept on #[cfg]

stats.rs:1398 (mut stat_args for stats-args cache deserialization). The little-endian arm consumes the String into a Vec<u8> while the big-endian arm borrows it. cfg_select!'s expression-form arms must be single expressions, so squeezing this through required restructuring ownership without a clear win.

Notes on cfg_select!

In expression position, brace-block arms must contain a single expression — multi-statement { let x = ...; expr } blocks trigger "trailing semicolon in macro" errors. Workaround for sniff_result (stats.rs) and validate.rs: pull the let mut outside cfg_select! (using #[cfg_attr(target_endian = "big", allow(unused_mut))] to suppress the cross-arch warning). Item-position usage works fine with multi-statement blocks (e.g., is_executable).

Test plan

  • cargo build --locked --bin qsv -F all_features — clean
  • cargo build --bin qsvlite -F lite — clean (only pre-existing describegpt.rs warning)
  • cargo build --bin qsvmcp -F qsvmcp — clean
  • cargo clippy --bin qsv -F all_features — clean
  • cargo test --test tests stats_cache -F all_features — 23 passed
  • cargo test --test tests test_validate -F all_features — 62 passed
  • cargo test --test tests test_schema -F all_features — 12 passed
  • cargo test --test tests test_tojsonl -F all_features — 25 passed
  • cargo test --test tests test_foreach -F all_features — 18 passed
  • cargo test --test tests test_pragmastat -F all_features — 47 passed
  • CI confirms big-endian arms still compile (s390x / mips builds)

🤖 Generated with Claude Code

…ites

Convert 15 sites across 9 files from paired #[cfg(...)] let-bindings and
duplicate-fn definitions to std's new cfg_select! macro, which reads like
a compile-time match. Notable wins: reset_sigpipe and is_executable
collapse from 2 fn defs to 1; the platform_factor 4-arm target_os ladder
becomes a single expression; the 9 target_endian simd_json/serde_json
pairs lose their #[cfg] scaffolding.

The stats.rs `mut stat_args` site (cache-args deserialization) is left
on #[cfg] — its little-endian arm consumes the String into a Vec<u8>
while the big-endian arm borrows it, and cfg_select!'s expression-form
arms can't carry multi-statement blocks without restructuring ownership.

Net -23 lines; cargo build/clippy clean; 187 tests across affected
commands pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented May 12, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity

Metric Results
Complexity 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Copy link
Copy Markdown
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 PR refactors platform-conditional code paths to use Rust 1.95’s cfg_select! macro, reducing duplicated #[cfg(...)] scaffolding and consolidating platform/endian-specific logic into single expressions/items.

Changes:

  • Replaced paired #[cfg] blocks with cfg_select! for OS-/endian-specific JSON (de)serialization and other platform conditionals.
  • Collapsed duplicated platform-specific implementations (e.g., reset_sigpipe, is_executable) into unified definitions.
  • Simplified a few platform-dependent constants/bindings (e.g., platform_factor, Windows row-count fallback, which vs where).

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/util.rs Consolidates unix/windows + endian-specific branches using cfg_select! (signals, executability, JSON paths, platform factors).
src/cmd/validate.rs Uses cfg_select! for endian-specific JSON schema deserialization.
src/cmd/tojsonl.rs Uses cfg_select! to select Windows row-count fallback implementation.
src/cmd/stats.rs Converts multiple endian-specific JSON serialization/deserialization sites to cfg_select!.
src/cmd/scoresql.rs Selects which vs where via cfg_select! for cross-platform PATH lookup.
src/cmd/schema.rs Uses cfg_select! for endian-specific JSON pretty-print serialization.
src/cmd/prompt.rs Uses cfg_select! to apply macOS-only dialog configuration without duplicating builder setup.
src/cmd/pragmastat.rs Uses cfg_select! for endian-specific cache record parsing.
src/cmd/foreach.rs Uses cfg_select! for unix/windows program path handling and dry-run string formatting.
src/cmd/apply.rs Refactors the thousands-formatting conditional branch structure.

Comment thread src/cmd/apply.rs Outdated
… flip

The clippy::if_not_else cleanup in e2a17bc flipped the branches but
left the explanatory comment describing the old `!= 0.0` flow. Rewrite
it to match the new condition (the `> 0.0` rationale about preserving
negative fractions is still relevant).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jqnatividad jqnatividad merged commit 5fdf8df into master May 12, 2026
15 checks passed
@jqnatividad jqnatividad deleted the cfg_select_macro branch May 12, 2026 12:17
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