Allow and document clippy::drain_collect - #8500
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe PR simplifies ChangesTail-call reference handling
Code-page error handling
Estimated code review effort: 1 (Trivial) | ~5 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/vm/src/vm/mod.rs`:
- Line 1471: Replace the remaining pending tail-call reference
`.drain(..).collect()` in the exception-unwind tail-call branch with the same
`mem::take` ownership transfer used by `initial_refs` and the analogous
branches. Preserve the existing behavior and run `cargo clippy` afterward.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro Plus
Run ID: 34fd5b4d-baa6-4d5c-b1e3-c931476ab6a6
📒 Files selected for processing (2)
crates/vm/src/stdlib/_codecs.rscrates/vm/src/vm/mod.rs
ShaharNaveh
left a comment
There was a problem hiding this comment.
great!
not a blocker: can we enforce this with the clippy rules if it's not already been done?
RustPython's tail call machinery pre-allocates and reuses a vector. The code drains the vector into a new vector which is stored elsewhere. Clippy warns that this pattern causes a spurious location. Clippy is usually right that this pattern is suspect, but in this case the initial vector is reused so we want to keep the initial location.
03bfd70 to
ef45416
Compare
|
@ShaharNaveh I went through the code in depth to check if the lint was necessary. It turns out that Clippy is overzealous here. The original code pre-allocates a buffer to reuse. The drain/collect idiom is intentional and correct. Instead, I documented the behavior and allowed the lint so that anyone else who encounters it knows not to fix the lint. I also fixed a totally unrelated but small lint. 😁 I used #[allow] instead of #[expect] because the lint doesn't trigger on my stable compiler but does trigger on nightly. CI would fail with #[expect]. |
tysm for looking into it! |
|
@joshuamegnauth54 can we still add this rule? so future places would get flagged |
ShaharNaveh
left a comment
There was a problem hiding this comment.
adding the rule is not a blocker (for me)
@coderabbitai Can |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '%s\n' '== Rust toolchain and project policy files =='
fd -HI -t f '^(rust-toolchain(\.toml)?|Cargo\.toml|clippy\.toml|CONTRIBUTING\.md|README\.md)$' . \
| sed -n '1,160p'
printf '%s\n' '== Compiler-version and nightly configuration references =='
rg -n -i -C 3 \
'(rust-version|rustup|nightly|stable|rustversion|check-cfg|unexpected_cfgs|drain_collect)' \
-g 'Cargo.toml' -g 'rust-toolchain*' -g 'clippy.toml' -g '*.rs' -g '*.md' . \
| sed -n '1,280p'
printf '%s\n' '== Tail-call source map =='
ast-grep outline crates/vm/src/vm/mod.rs --items all --match 'trampoline|tail|call' --view expanded || true
printf '%s\n' '== Changed lines in relevant files =='
git diff -- crates/vm/src/vm/mod.rs crates/vm/src/stdlib/_codecs.rsLength of output: 18513 🤖 web_search executed:
Length of output: 5066 🤖 web_search executed:
Length of output: 8212
This attribute is valid if the build defines a custom #[cfg_attr(nightly, expect(clippy::drain_collect))]
let refs: Vec<_> = refs.drain(..).collect();A build script or CI must then define println!("cargo::rustc-check-cfg=cfg(nightly)");
println!("cargo::rustc-cfg=nightly");The second line must run only when the selected I do not recommend this for this PR. RustPython selects stable in You are interacting with an AI system. |
|
|
@ShaharNaveh Do you mean enabling the lint or denying the lint? The lint should already be enabled by default, yet I'm unsure why it didn't trigger. Here's a Playground lint that demonstrates that it triggers on stable. 🤔 |
oh, it didn't trigger due to clippy bug, nvm... tysm! |
RustPython's tail call machinery pre-allocates and reuses a vector. The code drains the vector into a new vector which is stored elsewhere.
Clippy warns that this pattern causes a spurious location. Clippy is usually right that this pattern is suspect, but in this case the initial vector is reused so we want to keep the initial location.
Summary
Summary by CodeRabbit