Skip to content

chore(clippy): clear needless_borrow in niubash-runtime (#450, batch 1) - #469

Merged
caomengxuan666 merged 1 commit into
unixwin:masterfrom
Yuren0312:fix-450-needless-borrow-batch
Oct 20, 2026
Merged

caomengxuan666 merged 1 commit into
unixwin:masterfrom
Yuren0312:fix-450-needless-borrow-batch

Conversation

@Yuren0312

Copy link
Copy Markdown
Contributor

概述

niubash#450 分批清偿的第 1 批:needless_borrow 类别(按 issue 建议的「按 lint 类别聚合」分批方式,每批全量套件对照)。

根因

history.rs 的 lock_mut() 本身就返回 &mut HistoryState,但 11 处调用点写成 &mut state,实际构造出 &mut &mut HistoryState 再由编译器立即解引用——纯冗余借用:

函数 调用点
reload_from_file file_signature_lossy(&path, &mut state)
clear_memory refresh_if_stale(..., &mut state, ...) ×1 + file_signature_lossy(..., &mut state) ×3
save refresh_if_stale(...) ×1 + sync_state(&mut state) ×1
update / clear / delete refresh_if_stale(..., &mut state, ...) 各 1

plugins/assets.rs 的 3 处 rc 夹具则把 &str 常量 OMB_BLOCK 又多套了一层引用,传给签名本就是 (id: &str, body: &str) 的 seed_legacy_block。

冗余借位去掉后,6 个 let mut state = self.lock_mut()?; 绑定不再需要 mut(rustc unused_mut 证实)——一并移除;否则只是把 needless_borrow 换成 unused_mut,对照基线不降反升。

改动

  • crates/niubash-runtime/src/history.rs:11 处 &mut state → state(隐式 reborrow,语义等价)+ 6 处 let mut state → let state
  • crates/niubash-runtime/src/plugins/assets.rs:3 处 &OMB_BLOCK → OMB_BLOCK
  • CHANGELOG.md:[Unreleased] / Changed 追加条目

共 3 文件、源码 20 行、changelog 1 行;Cargo.lock 未动;纯 LF;无其他文件改动、无格式化夹带。

验证

环境:rustc/cargo 1.99.0 stable、x86_64-pc-windows-gnu(与 issue 的 1.99 口径一致);rubash 按仓库 [patch] 用 sibling master 源码构建(与 CI 同一机制)。

  1. clippy(cargo clippy -p niubash-runtime --all-targets,修复前后逐位置集合比对):
    • needless_borrow:14 → 0(history.rs 11 + assets.rs 3)
    • 唯一告警位置总数:119 → 105(−14);「新增告警位置」差分集合为空(去掉 borrow 后同步去 mut,没有把 lint 从一类搬到另一类)
  2. 测试(cargo test -p niubash-runtime --lib):558 passed / 3 failed;与未打补丁 master 基线失败集合逐项一致——3 个失败均为 plugins::distros::tests::* 需要 git clone GitHub 仓库的网络用例(本沙箱无外网),与本改动无关
  3. 直接覆盖改动行的 3 个用例全部 ok:duplicate_legacy_blocks_are_reported_and_left_to_the_user、corrupt_owner_rc_is_preserved_byte_identically_through_plugin_ops、moved_out_guard_region_stays_byte_stable_across_verb_loops
  4. cargo fmt --check --all 干净;cargo check -p niubash-runtime --all-targets 编译通过
  5. workspace 全量套件(cargo test --workspace):与未打补丁 master 基线结果一致(见下方「环境说明」)

环境说明

本沙箱构建 root crate 需要两个本地垫片(均与本次改动无关,CI 的 windows-latest runner 无需):

  • rc.exe:沙箱无 Windows SDK 资源编译器,用 windres 垫片转换 MS rc CLI 生成图标 .res
  • 3 个 plugins::distros::tests::* 失败:需要 git clone github.com(沙箱无外网)

console_window_gate 集成测试的 15 个 detached_* 用例在本环境计数到沙箱自身的 conhost——在未打补丁的 master 上同样失败(逐项一致),与本改动无关。

剩余类别(供后续批次,issue 约定按类别聚合、每批全量套件对照)

共 105 个唯一告警位置:cloned_ref_to_slice_refs(11)、question_mark(7)、chunks_exact_to_as_chunks(6)、redundant_closure(6)、unnecessary_map_or(5)、doc_lazy_continuation(5)、needless_option_as_deref(5)、derivable_impls(4)、write_with_newline(4)、unnecessary_to_owned(4)、manual_pattern_char_comparison(3)、let_unit_value(3)、cmp_owned(3),以及 duplicated_attributes/manual_strip/for_kv_map/manual_c_str_literals/needless_return/manual_contains/items_after_test_module/redundant_closure_call/unnecessary_mut_passed 各 2,其余单项若干。

关于 issue 的「CI clippy 门评估」:建议两步走——先上「warning 计数不增门」(以本批后的 105 个唯一位置为钉死基线,PR 不得引入新告警),待各批次清偿完再切 -D warnings。这样在债务清零前就能防回归,不必等全部批次落地。

…batch 1)

history.rs passed `&mut state` (a `&mut HistoryState` already returned by
lock_mut()) to refresh_if_stale/sync_state/file_signature_lossy at 11 call
sites, building a `&mut &mut HistoryState` the compiler immediately
dereferenced; plugins/assets.rs passed `&OMB_BLOCK` (a `&str` const) to
seed_legacy_block in 3 rc fixtures. With the redundant borrows gone the six
`let mut state` bindings no longer need `mut` either, so it goes too.

cargo clippy -p niubash-runtime --all-targets: 14 fewer unique warning
sites (119 -> 105), needless_borrow 14 -> 0, no new lint introduced.
Runtime lib suite: 558 passed / 3 failed, failure set identical to
unpatched master (3 network-dependent distro tests).

Signed-off-by: Yuren0312 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

欢迎!这是你的第一个 PR 🎉 请确认已跑过本地测试套件;CI 通过后维护者会 review。

@caomengxuan666

Copy link
Copy Markdown
Member

欢迎 @Yuren0312,感谢接下 #450 的第一批清偿(needless_borrow)——正是票里「按 lint 类别聚合、分批清偿」的打法,batch 1 选这个类别很适合起步。

评审正在进行:逐 hunk 核对行为中立性、clippy 前后计数对照(#450 的验收口径是 clippy --all-targets 计数归零或钉死基线不增)、以及 history / plugins::assets 邻域的定向测试。首次贡献的 CI 需要维护者放行,我们审核时会一并触发并贴结果。

有任何评审意见都会留在本 PR,跟评即可,期待后续批次。

@caomengxuan666 caomengxuan666 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Review: APPROVE(batch 1 needless_borrow,行为中立性复核通过)

逐 hunk 复核结论——全部为纯冗余借用移除,无语义夹带:

  • history.rs:11 处 &mut state → state。lock_mut() 本身返回 &mut HistoryState(history.rs:438),file_signature_lossy / refresh_if_stale / sync_state 形参均为 &mut HistoryState,原写法经 deref coercion 构造 &mut &mut HistoryState 再立即解引用,去掉后为隐式 reborrow,语义等价;伴随的 6 处 let mut state → let state 正确(绑定不再被重赋值,经借用修 mutation 仍合法,unused_mut 未回流)。
  • plugins/assets.rs:3 处 &OMB_BLOCK → OMB_BLOCK,const OMB_BLOCK: &str 传入 seed_legacy_block(id: &str, body: &str),纯 &&str 去层。
  • CHANGELOG.md:条目落在 [Unreleased] / ### Changed,位置与措辞与实际改动一致。
  • 无变量重绑 / 生命周期变化 / 借用改移动;diff 之外零改动,Cargo.lock 未动。

clippy 前后计数(本机 rustc/cargo 1.99.0 stable,clippy -p niubash-runtime --all-targets,PR vs origin/master@6303394)

口径 master PR Δ
clippy::needless_borrow(raw 告警条数) 25 0 −25
clippy::needless_borrow(唯一位置) 14 0 −14
唯一告警位置总数 119 105 −14
其它类别(逐一比对) — — 0 变化
PR 分支新增告警位置 — 0 —

唯一发生变化的 lint 类别就是 needless_borrow,与描述完全一致。needless_borrow 计数归零,符合 #450 对 batch 1 的验收口径(剩余 1 处 needless_borrows_for_generic_args 是另一个 lint 类别,留给后续批次)。

本地验证

  • cargo test -p niubash-runtime history:24 passed / 0 failed
  • cargo test -p niubash-runtime assets:25 passed / 0 failed(含直接覆盖改动行的 duplicate_legacy_blocks_are_reported_and_left_to_the_user、corrupt_owner_rc_is_preserved_byte_identically_through_plugin_ops、moved_out_guard_region_stays_byte_stable_across_verb_loops,均 ok)
  • cargo fmt --check --all:干净

CI(已放行 run 39409866909)

7/8 job 绿(cross-check ubuntu/macos、cross-target-check ×4、cross-target-smoke)。唯一失败 build-and-test / cargo test (workspace):detached_builtin_only_session_allocates_zero_conhosts。与 merge-base 6303394 自身的 CI(run 39406280068)逐字相同——同 job、同 step、同用例、同 17 passed/1 failed,且 master 最近 5 次 CI 全部 failure:属 master 既有红(conhost 计数用例),非本 PR 引入,不构成本 PR 的回归,也不该由 batch 1 顺手修(避免夹带)。

按既有纪律 squash 合并,感谢贡献!剩余 105 个唯一告警位置的后续批次与「warning 计数不增门」建议,欢迎继续按 #450 推进。

@caomengxuan666
caomengxuan666 merged commit 1b3749e into unixwin:master Oct 20, 2026
10 of 11 checks passed
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