Skip to content

Commit eeabacc

Browse files
committed
Avoid clone
1 parent ddd4764 commit eeabacc

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

crates/ty_python_semantic/src/semantic_index/builder.rs

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

815-
let snapshot = self.use_def_maps[scope].snapshot();
816-
self.try_node_context_stack_manager
817-
.record_definition(scope_index, &snapshot);
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+
}
818823

819824
(definition, num_definitions)
820825
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ 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+
4048
/// Record a definition in the try-node context at `scope_index`.
4149
///
4250
/// For each active `try` block in that scope's context stack, a clone of `snapshot`

0 commit comments

Comments
 (0)