Skip to content

Commit 956664d

Browse files
committed
Avoid clone
1 parent eeabacc commit 956664d

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

crates/ty_python_semantic/src/semantic_index/builder.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -812,14 +812,8 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast> {
812812
}
813813
}
814814

815-
if self
816-
.try_node_context_stack_manager
817-
.has_active_try_context(scope_index)
818-
{
819-
let snapshot = self.use_def_maps[scope].snapshot();
820-
self.try_node_context_stack_manager
821-
.record_definition(scope_index, &snapshot);
822-
}
815+
self.try_node_context_stack_manager
816+
.record_definition(scope_index, &self.use_def_maps[scope]);
823817

824818
(definition, num_definitions)
825819
}

crates/ty_python_semantic/src/semantic_index/builder/except_handlers.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::semantic_index::use_def::FlowSnapshot;
1+
use crate::semantic_index::use_def::{FlowSnapshot, UseDefMapBuilder};
22

33
/// An abstraction over the fact that each scope should have its own [`TryNodeContextStack`]
44
#[derive(Debug, Default)]
@@ -37,22 +37,17 @@ impl TryNodeContextStackManager {
3737
self.current_try_context_stack().pop_context()
3838
}
3939

40-
/// Returns `true` if the scope at `scope_index` is currently inside at least
41-
/// one `try` block and therefore needs definition snapshots recorded.
42-
pub(super) fn has_active_try_context(&self, scope_index: usize) -> bool {
43-
self.0
44-
.get(scope_index)
45-
.is_some_and(|stack| !stack.0.is_empty())
46-
}
47-
4840
/// Record a definition in the try-node context at `scope_index`.
4941
///
50-
/// For each active `try` block in that scope's context stack, a clone of `snapshot`
51-
/// is pushed so that `except`/`finally` handlers can model which definitions may
52-
/// have been reached before an exception.
53-
pub(super) fn record_definition(&mut self, scope_index: usize, snapshot: &FlowSnapshot) {
42+
/// A snapshot is created from `use_def_map` only if the scope has at least
43+
/// one active `try` block; otherwise this is a no-op.
44+
pub(super) fn record_definition(
45+
&mut self,
46+
scope_index: usize,
47+
use_def_map: &UseDefMapBuilder<'_>,
48+
) {
5449
if let Some(stack) = self.0.get_mut(scope_index) {
55-
stack.record_definition(snapshot);
50+
stack.record_definition(use_def_map);
5651
}
5752
}
5853

@@ -87,10 +82,13 @@ impl TryNodeContextStack {
8782
try_suite_snapshots
8883
}
8984

90-
/// For each `try` block on the stack, push the snapshot onto the `try` block
91-
fn record_definition(&mut self, snapshot: &FlowSnapshot) {
85+
/// For each `try` block on the stack, create a snapshot and push it.
86+
///
87+
/// The snapshot is created lazily per context, so this is a no-op when
88+
/// no `try` block is active.
89+
fn record_definition(&mut self, use_def_map: &UseDefMapBuilder<'_>) {
9290
for context in &mut self.0 {
93-
context.record_definition(snapshot.clone());
91+
context.record_definition(use_def_map.snapshot());
9492
}
9593
}
9694
}

0 commit comments

Comments
 (0)