Skip to content

Commit

Permalink
Don't flag redefined-while-unsed in if branches
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 7, 2024
1 parent f684175 commit 15bbe34
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
12 changes: 6 additions & 6 deletions crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {

// If the bindings are in different forks, abort.
if shadowed.source.map_or(true, |left| {
binding.source.map_or(true, |right| {
checker.semantic.different_branches(left, right)
})
binding
.source
.map_or(true, |right| !checker.semantic.same_branch(left, right))
}) {
continue;
}
Expand Down Expand Up @@ -233,9 +233,9 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {

// If the bindings are in different forks, abort.
if shadowed.source.map_or(true, |left| {
binding.source.map_or(true, |right| {
checker.semantic.different_branches(left, right)
})
binding
.source
.map_or(true, |right| !checker.semantic.same_branch(left, right))
}) {
continue;
}
Expand Down
9 changes: 3 additions & 6 deletions crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,11 +1163,11 @@ impl<'a> SemanticModel<'a> {
false
}

/// Returns `true` if `left` and `right` are on different branches of an `if`, `match`, or
/// Returns `true` if `left` and `right` are in the same branches of an `if`, `match`, or
/// `try` statement.
///
/// This implementation assumes that the statements are in the same scope.
pub fn different_branches(&self, left: NodeId, right: NodeId) -> bool {
pub fn same_branch(&self, left: NodeId, right: NodeId) -> bool {
// Collect the branch path for the left statement.
let left = self
.nodes
Expand All @@ -1184,10 +1184,7 @@ impl<'a> SemanticModel<'a> {
.flat_map(|branch_id| self.branches.ancestor_ids(*branch_id))
.collect::<Vec<_>>();

!left
.iter()
.zip(right.iter())
.all(|(left, right)| left == right)
left == right
}

/// Returns `true` if the given expression is an unused variable, or consists solely of
Expand Down

0 comments on commit 15bbe34

Please sign in to comment.