Skip to content

perf: Fix clippy::drain_collect - #8500

Draft
joshuamegnauth54 wants to merge 1 commit into
RustPython:mainfrom
joshuamegnauth54:clippy-drain_collect
Draft

perf: Fix clippy::drain_collect#8500
joshuamegnauth54 wants to merge 1 commit into
RustPython:mainfrom
joshuamegnauth54:clippy-drain_collect

Conversation

@joshuamegnauth54

@joshuamegnauth54 joshuamegnauth54 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixes a clippy lint about collecting a drain iterator.

I noticed this lint while fixing up my last patch. It seems reasonable, but I'll rely on CodeRabbit to check if it's spurious in which case I'll just expect the lint.

Summary by CodeRabbit

  • Refactor
    • Simplified internal character encoding logic without changing encoding behavior.
    • Improved tail-call reference handling for more efficient runtime execution.
  • Bug Fixes
    • Preserved pending references correctly across initial calls, tail calls, and resumed calls.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR simplifies wchar_len calculation in codec error handling and updates the VM trampoline to move pending tail-call references with mem::take.

Changes

Tail-call reference ownership

Layer / File(s) Summary
Trampoline reference moves
crates/vm/src/vm/mod.rs
The trampoline moves initial, callee-generated, and caller-generated pending references with mem::take instead of draining and collecting them.

Code-page error handling

Layer / File(s) Summary
Wchar length calculation
crates/vm/src/stdlib/_codecs.rs
wchar_len now uses a direct conditional expression. Surrogate handling and UTF-16 length calculation remain unchanged.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: youknowone

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: replacing the pattern flagged by the clippy::drain_collect lint.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 81df1ff and 03bfd70.

📒 Files selected for processing (2)
  • crates/vm/src/stdlib/_codecs.rs
  • crates/vm/src/vm/mod.rs

Comment thread crates/vm/src/vm/mod.rs
let initial_refs = unsafe { &mut *self.pending_tailcall_refs.get() }
.drain(..)
.collect();
let initial_refs = mem::take(unsafe { &mut *self.pending_tailcall_refs.get() });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

if rg -n -U '\.drain\(\.\.\)\s*\.collect\(\)' crates/vm/src/vm/mod.rs; then
  echo "A drain(..).collect() call remains." >&2
  exit 1
fi

Repository: RustPython/RustPython

Length of output: 308


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- relevant source ---'
sed -n '1450,1565p' crates/vm/src/vm/mod.rs
sed -n '1585,1625p' crates/vm/src/vm/mod.rs

printf '%s\n' '--- all drain(..).collect() patterns in the file ---'
rg -n -U -C 2 '\.drain\(\.\.\)\s*\.collect\(\)' crates/vm/src/vm/mod.rs || true

printf '%s\n' '--- mem::take usage and pending-tailcall references ---'
rg -n -C 2 'pending_tailcall_refs|mem::take' crates/vm/src/vm/mod.rs

Repository: RustPython/RustPython

Length of output: 10658


Replace the remaining .drain(..).collect() call with mem::take.

The exception-unwind tail-call branch still uses this pattern at lines 1607–1609. Apply the same ownership transfer used at lines 1471, 1500, and 1548, then run cargo clippy.

🤖 Prompt for 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.

In `@crates/vm/src/vm/mod.rs` at 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.

Source: Coding guidelines

@joshuamegnauth54
joshuamegnauth54 marked this pull request as draft August 12, 2026 02:07
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.

1 participant