Skip to content

Commit 3a98ef7

Browse files
Allow and document clippy::drain_collect (RustPython#8500)
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.
1 parent e02e215 commit 3a98ef7

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

crates/vm/src/stdlib/_codecs.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -791,19 +791,18 @@ mod _codecs_windows {
791791

792792
// Convert code point to UTF-16
793793
let mut wchars = [0u16; 2];
794-
let wchar_len;
795794
let is_surrogate = (0xD800..=0xDFFF).contains(&ch);
796795

797-
if is_surrogate {
798-
wchar_len = 0; // Can't encode surrogates normally
796+
let wchar_len = if is_surrogate {
797+
0 // Can't encode surrogates normally
799798
} else if ch < 0x10000 {
800799
wchars[0] = ch as u16;
801-
wchar_len = 1;
800+
1
802801
} else {
803802
wchars[0] = ((ch - 0x10000) >> 10) as u16 + 0xD800;
804803
wchars[1] = ((ch - 0x10000) & 0x3FF) as u16 + 0xDC00;
805-
wchar_len = 2;
806-
}
804+
2
805+
};
807806

808807
if !is_surrogate {
809808
let mut buf = [0u8; 8];

crates/vm/src/vm/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,10 @@ impl VirtualMachine {
14671467

14681468
let initial_ptr = self.take_pending_tailcall();
14691469
// Drain the refs that keep the initial callee's raw pointers alive.
1470+
#[allow(
1471+
clippy::drain_collect,
1472+
reason = "`pending_tailcall_refs`'s allocation is intentionally reused"
1473+
)]
14701474
let initial_refs = unsafe { &mut *self.pending_tailcall_refs.get() }
14711475
.drain(..)
14721476
.collect();
@@ -1498,6 +1502,10 @@ impl VirtualMachine {
14981502
let result = crate::frame::run_iframe(callee, self);
14991503
match result {
15001504
Ok(ExecutionResult::TailCall) => {
1505+
#[allow(
1506+
clippy::drain_collect,
1507+
reason = "`pending_tailcall_refs`'s allocation is intentionally reused"
1508+
)]
15011509
let refs = unsafe { &mut *self.pending_tailcall_refs.get() }
15021510
.drain(..)
15031511
.collect();
@@ -1548,6 +1556,10 @@ impl VirtualMachine {
15481556
let result = crate::frame::run_iframe(caller_iframe, self);
15491557
match result {
15501558
Ok(ExecutionResult::TailCall) => {
1559+
#[allow(
1560+
clippy::drain_collect,
1561+
reason = "`pending_tailcall_refs`'s allocation is intentionally reused"
1562+
)]
15511563
let refs = unsafe { &mut *self.pending_tailcall_refs.get() }
15521564
.drain(..)
15531565
.collect();
@@ -1609,6 +1621,10 @@ impl VirtualMachine {
16091621
let result = crate::frame::run_iframe(caller_iframe, self);
16101622
match result {
16111623
Ok(ExecutionResult::TailCall) => {
1624+
#[allow(
1625+
clippy::drain_collect,
1626+
reason = "`pending_tailcall_refs`'s allocation is intentionally reused"
1627+
)]
16121628
let refs = unsafe { &mut *self.pending_tailcall_refs.get() }
16131629
.drain(..)
16141630
.collect();

0 commit comments

Comments
 (0)