Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 28, 2023
1 parent 9d243cd commit 10bb124
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
24 changes: 12 additions & 12 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ where
if self.settings.rules.enabled(Rule::UnusedLoopControlVariable) {
self.deferred
.for_loops
.push((stmt, (self.ctx.scope_id, self.ctx.parents.clone())));
.push((stmt, (self.ctx.scope_id(), self.ctx.parents.clone())));
}
if self
.settings
Expand Down Expand Up @@ -1983,7 +1983,7 @@ where
self.deferred.definitions.push((
definition,
scope.visibility,
(self.ctx.scope_id, self.ctx.parents.clone()),
(self.ctx.scope_id(), self.ctx.parents.clone()),
));
self.ctx.visible_scope = scope;

Expand Down Expand Up @@ -2021,7 +2021,7 @@ where

self.deferred.functions.push((
stmt,
(self.ctx.scope_id, self.ctx.parents.clone()),
(self.ctx.scope_id(), self.ctx.parents.clone()),
self.ctx.visible_scope,
));
}
Expand All @@ -2046,7 +2046,7 @@ where
self.deferred.definitions.push((
definition,
scope.visibility,
(self.ctx.scope_id, self.ctx.parents.clone()),
(self.ctx.scope_id(), self.ctx.parents.clone()),
));
self.ctx.visible_scope = scope;

Expand Down Expand Up @@ -2261,13 +2261,13 @@ where
expr.range(),
value,
(self.ctx.in_annotation, self.ctx.in_type_checking_block),
(self.ctx.scope_id, self.ctx.parents.clone()),
(self.ctx.scope_id(), self.ctx.parents.clone()),
));
} else {
self.deferred.type_definitions.push((
expr,
(self.ctx.in_annotation, self.ctx.in_type_checking_block),
(self.ctx.scope_id, self.ctx.parents.clone()),
(self.ctx.scope_id(), self.ctx.parents.clone()),
));
}
return;
Expand Down Expand Up @@ -3495,7 +3495,7 @@ where
expr.range(),
value,
(self.ctx.in_annotation, self.ctx.in_type_checking_block),
(self.ctx.scope_id, self.ctx.parents.clone()),
(self.ctx.scope_id(), self.ctx.parents.clone()),
));
}
if self
Expand Down Expand Up @@ -3618,7 +3618,7 @@ where
ExprKind::Lambda { .. } => {
self.deferred
.lambdas
.push((expr, (self.ctx.scope_id, self.ctx.parents.clone())));
.push((expr, (self.ctx.scope_id(), self.ctx.parents.clone())));
}
ExprKind::IfExp { test, body, orelse } => {
visit_boolean_test!(self, test);
Expand Down Expand Up @@ -4356,7 +4356,7 @@ impl<'a> Checker<'a> {
if let Some(index) = scope.get(id.as_str()) {
// Mark the binding as used.
let context = self.ctx.execution_context();
self.ctx.bindings[*index].mark_used(self.ctx.scope_id, expr.range(), context);
self.ctx.bindings[*index].mark_used(self.ctx.scope_id(), expr.range(), context);

if self.ctx.bindings[*index].kind.is_annotation()
&& self.ctx.in_deferred_string_type_definition.is_none()
Expand Down Expand Up @@ -4386,7 +4386,7 @@ impl<'a> Checker<'a> {
// Mark the sub-importation as used.
if let Some(index) = scope.get(full_name) {
self.ctx.bindings[*index].mark_used(
self.ctx.scope_id,
self.ctx.scope_id(),
expr.range(),
context,
);
Expand All @@ -4403,7 +4403,7 @@ impl<'a> Checker<'a> {
// Mark the sub-importation as used.
if let Some(index) = scope.get(full_name.as_str()) {
self.ctx.bindings[*index].mark_used(
self.ctx.scope_id,
self.ctx.scope_id(),
expr.range(),
context,
);
Expand Down Expand Up @@ -4722,7 +4722,7 @@ impl<'a> Checker<'a> {
docstring,
},
self.ctx.visible_scope.visibility,
(self.ctx.scope_id, self.ctx.parents.clone()),
(self.ctx.scope_id(), self.ctx.parents.clone()),
));
docstring.is_some()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pylint/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn in_dunder_init(checker: &Checker) -> bool {
if name != "__init__" {
return false;
}
let Some(parent) = checker.ctx.parent_scope() else {
let Some(parent) = scope.parent.map(|scope_id| &checker.ctx.scopes[scope_id]) else {
return false;
};

Expand Down
12 changes: 4 additions & 8 deletions crates/ruff_python_semantic/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl<'a> Context<'a> {

/// Push a [`Scope`] with the given [`ScopeKind`] onto the stack.
pub fn push_scope(&mut self, kind: ScopeKind<'a>) {
let id = self.scopes.push_scope(self.scope_id, kind);
let id = self.scopes.push_scope(kind, self.scope_id);
self.scope_id = id;
}

Expand Down Expand Up @@ -390,7 +390,7 @@ impl<'a> Context<'a> {
}

/// Returns the id of the top-most scope
pub fn scope_id(&self) -> ScopeId {
pub const fn scope_id(&self) -> ScopeId {
self.scope_id
}

Expand All @@ -399,16 +399,12 @@ impl<'a> Context<'a> {
&mut self.scopes[self.scope_id]
}

pub fn parent_scope(&self) -> Option<&Scope> {
self.scopes[self.scope_id]
.parent
.map(|index| &self.scopes[index])
}

/// Returns an iterator over all scopes, starting from the current scope.
pub fn scopes(&self) -> impl Iterator<Item = &Scope> {
self.scopes.ancestor_scopes(self.scope_id)
}

/// Returns `true` if the context is in an exception handler.
pub const fn in_exception_handler(&self) -> bool {
self.in_exception_handler
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<'a> Scopes<'a> {
}

/// Pushes a new scope and returns its unique id
pub fn push_scope(&mut self, parent: ScopeId, kind: ScopeKind<'a>) -> ScopeId {
pub fn push_scope(&mut self, kind: ScopeKind<'a>, parent: ScopeId) -> ScopeId {
let next_id = ScopeId::try_from(self.0.len()).unwrap();
self.0.push(Scope::local(next_id, parent, kind));
next_id
Expand Down

0 comments on commit 10bb124

Please sign in to comment.