Skip to content

fix(cosh-ng): swap chunks_exact for as_chunks - #2742

Open
jfeng18 wants to merge 1 commit into
mainfrom
fix/cosh-ng/as-chunks-lint
Open

fix(cosh-ng): swap chunks_exact for as_chunks#2742
jfeng18 wants to merge 1 commit into
mainfrom
fix/cosh-ng/as-chunks-lint

Conversation

@jfeng18

@jfeng18 jfeng18 commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Why

Clippy 1.98 (rolling stable toolchain) ships the new chunks_exact_to_as_chunks lint. It fires on pre-existing decode_hex code in cosh-platform, turning the fast-checks job red on PRs that run stable clippy (e.g. #2708).

What changed

Swapped .chunks_exact(2) for .as_chunks::<2>().0.iter() in decode_hex — the exact form clippy suggests. Semantics-preserving: both iterate full 2-byte chunks and drop the tail remainder; the length-parity check above guarantees the remainder is always empty here.

Related issue

no-issue: pre-existing code flagged by a new clippy lint, not a user-visible bug. Unblocks the #2708 fast-checks job. Attribution: Fixes: 1a935895a391e9714e000fab00c3602a6580cd79 ("feat(cosh-ng): [core,shell] add audit log") in the commit body per repo convention.

User / Agent impact

None. Internal API-equivalent refactor of a private hex decoder.

Risk and compatibility

  • Public CLI, API, configuration, or documented behavior changed
  • Privileged or security-sensitive behavior changed
  • Cross-component contract changed
  • Migration or rollback guidance is needed

Low risk: as_chunks stabilized in Rust 1.88 and cosh-ng CI pins 1.89, so the pinned-toolchain job compiles unchanged.

Validation

  • Reproduced pre-fix: stable 1.98 cargo clippy -p cosh-platform --all-targets -- -D warnings fails at query.rs:629; post-fix exit 0
  • ECS (Linux, RUSTUP_TOOLCHAIN=1.89.0): cargo clippy -p cosh-platform --all-targets -- -D warnings clean; cargo test -p cosh-platform 311 passed / 0 failed
  • cargo fmt --all --check clean under both 1.89 and stable 1.98
  • Equivalence harness (rustc 1.89, not committed): old and new decode_hex agree on all 484 hex-pair inputs plus 10 boundary inputs (empty, odd-length, non-hex)

Documentation and rollback

No docs change. Rollback = revert the single commit.

Clippy 1.98 on stable rolls the new chunks_exact_to_as_chunks lint
onto pre-existing decode_hex code, turning the fast-checks job on
#2708 red. as_chunks::<2>().0.iter() is semantics-preserving: same
full chunks, same dropped tail remainder as chunks_exact(2).
Fixes: 1a93589 ("feat(cosh-ng): [core,shell] add audit log")
Assisted-by: Qoder
Signed-off-by: Jiangtian Feng <jiangtianf97@163.com>
@jfeng18
jfeng18 requested a review from KaiLongZhou as a code owner August 21, 2026 06:45
@github-actions github-actions Bot added the component:cosh-ng src/cosh-ng label Aug 21, 2026

@qoderai qoderai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本次修改范围内未发现功能或安全缺陷。唯一变更为遵循新 clippy lint 的等价重构,无需额外改动。


🤖 Generated by QoderView workflow run

Comment thread src/cosh-ng/crates/cosh-platform/src/audit/query.rs
@KaiLongZhou

Copy link
Copy Markdown
Collaborator

Code Review: fix(cosh-ng): swap chunks_exact for as_chunks

Verdict: LGTM

Correctness

变更语义等价,分析如下:

  • chunks_exact(2) 产生 &[u8] 迭代器(每片长度 2,丢弃余数)
  • as_chunks::<2>().0.iter() 产生 &[u8; 2] 迭代器(同样丢弃余数)
  • 上层 value.len() & 1 == 1 的奇偶校验保证余数始终为空,两种写法处理完全相同的输入
  • pair[0] / pair[1] 索引在 &[u8]&[u8; 2] 上行为一致

变换是纯机械性的 clippy 建议替换,无逻辑变更。

观察(非阻塞)

  1. MSRV 隐式提升:workspace Cargo.toml 声明 rust-version = "1.74",但 as_chunks 在 Rust 1.88 才稳定。虽然 CI 实际使用 stable(当前 1.98),但如果有外部用户按 MSRV 1.74 编译 cosh-platform 会失败。建议后续统一更新 rust-version 字段或确认 MSRV 仅为 aspirational。

  2. 同 lint 其他触发点:仓库中还有两处 chunks_exact(2) 可能在同一 clippy 版本下被标记:

    • cosh-core/src/tool/grep.rs:511decode_utf16_bytes 中的 chunks_exact(2)
    • cosh-shell/src/recommendation/personal_crypto.rs:259 — 测试辅助 decode_hex 中的 chunks_exact(2)

    本 PR 仅修复 cosh-platform,如果 fast-checks 只跑 cargo clippy -p cosh-platform 则足够;若后续 workspace 级别 clippy 也报错,需要追加修复。

  3. 可读性.as_chunks::<2>().0.iter().chunks_exact(2) 稍显晦意(.0 是 tuple struct access),但这是 clippy 推荐形式且提供了更强的静态保证([u8; 2] vs [u8]),可接受。

总结

单文件、单函数、机械性 lint 修复,风险极低。验证充分(PR 描述中列出了等价性测试)。LGTM。

@jfeng18

jfeng18 commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the thorough review and the LGTM!

Confirming the follow-up plan for the non-blocking observations:

  • MSRV declaration alignment: bump workspace rust-version from 1.74 to 1.88 to match the as_chunks stabilization.
  • The two remaining chunks_exact(2) sites — cosh-core/src/tool/grep.rs:511 and cosh-shell/src/recommendation/personal_crypto.rs:259 — will be swapped in the same follow-up PR.

It is being prepared right now and should land shortly.

One small ask: you are one of the requested reviewers, and the branch ruleset requires an approval before this PR can merge — would you mind converting your verbal LGTM into a formal review approval when convenient? No rush, just so the PR does not sit idle on that requirement. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants