mirror of https://github.com/astral-sh/ruff
Treat each match case as a separate scope
This commit is contained in:
parent
d93c5811ea
commit
005d3b9525
|
|
@ -14,13 +14,26 @@ def f():
|
||||||
print(model)
|
print(model)
|
||||||
case Car(make, "Corolla"):
|
case Car(make, "Corolla"):
|
||||||
print(make)
|
print(make)
|
||||||
|
case Car(_, _):
|
||||||
|
print(make, model)
|
||||||
|
case _:
|
||||||
|
print("No match")
|
||||||
|
|
||||||
|
|
||||||
def f(provided: int) -> int:
|
def f(provided: int) -> int:
|
||||||
match provided:
|
match provided:
|
||||||
case True:
|
case True:
|
||||||
return captured # F821
|
return captured # F821
|
||||||
|
case [*_] as captured:
|
||||||
|
return captured
|
||||||
case [captured, *_]:
|
case [captured, *_]:
|
||||||
return captured
|
return captured
|
||||||
case captured:
|
case captured:
|
||||||
return captured
|
return captured
|
||||||
|
|
||||||
|
|
||||||
|
match 1:
|
||||||
|
case 1:
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
print(x)
|
||||||
|
|
|
||||||
|
|
@ -119,5 +119,7 @@ def f(x: int):
|
||||||
print("A")
|
print("A")
|
||||||
case [Bar.A, *_]:
|
case [Bar.A, *_]:
|
||||||
print("A")
|
print("A")
|
||||||
|
case [*_] as y:
|
||||||
|
z = 1
|
||||||
case y:
|
case y:
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,7 @@ pub enum ScopeKind<'a> {
|
||||||
Generator,
|
Generator,
|
||||||
Module,
|
Module,
|
||||||
Lambda(Lambda<'a>),
|
Lambda(Lambda<'a>),
|
||||||
|
Case,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,8 @@ use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use rustpython_common::cformat::{CFormatError, CFormatErrorType};
|
use rustpython_common::cformat::{CFormatError, CFormatErrorType};
|
||||||
use rustpython_parser::ast::{
|
use rustpython_parser::ast::{
|
||||||
Arg, Arguments, Comprehension, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext,
|
Arg, Arguments, Comprehension, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext,
|
||||||
ExprKind, KeywordData, Located, Location, Operator, Pattern, PatternKind, Stmt, StmtKind,
|
ExprKind, KeywordData, Located, Location, MatchCase, Operator, Pattern, PatternKind, Stmt,
|
||||||
Suite,
|
StmtKind, Suite,
|
||||||
};
|
};
|
||||||
use rustpython_parser::parser;
|
use rustpython_parser::parser;
|
||||||
use smallvec::smallvec;
|
use smallvec::smallvec;
|
||||||
|
|
@ -29,7 +29,7 @@ use crate::ast::types::{
|
||||||
RefEquality, Scope, ScopeKind,
|
RefEquality, Scope, ScopeKind,
|
||||||
};
|
};
|
||||||
use crate::ast::typing::{match_annotated_subscript, Callable, SubscriptKind};
|
use crate::ast::typing::{match_annotated_subscript, Callable, SubscriptKind};
|
||||||
use crate::ast::visitor::{walk_excepthandler, walk_pattern, Visitor};
|
use crate::ast::visitor::{walk_excepthandler, walk_match_case, walk_pattern, Visitor};
|
||||||
use crate::ast::{branch_detection, cast, helpers, operations, typing, visitor};
|
use crate::ast::{branch_detection, cast, helpers, operations, typing, visitor};
|
||||||
use crate::docstrings::definition::{Definition, DefinitionKind, Docstring, Documentable};
|
use crate::docstrings::definition::{Definition, DefinitionKind, Docstring, Documentable};
|
||||||
use crate::registry::{Diagnostic, Rule};
|
use crate::registry::{Diagnostic, Rule};
|
||||||
|
|
@ -3841,6 +3841,14 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn visit_match_case(&mut self, match_case: &'b MatchCase) {
|
||||||
|
self.push_scope(Scope::new(ScopeKind::Case));
|
||||||
|
walk_match_case(self, match_case);
|
||||||
|
self.deferred_assignments
|
||||||
|
.push((self.scope_stack.clone(), self.parents.clone()));
|
||||||
|
self.pop_scope();
|
||||||
|
}
|
||||||
|
|
||||||
fn visit_pattern(&mut self, pattern: &'b Pattern) {
|
fn visit_pattern(&mut self, pattern: &'b Pattern) {
|
||||||
if let PatternKind::MatchAs {
|
if let PatternKind::MatchAs {
|
||||||
name: Some(name), ..
|
name: Some(name), ..
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,36 @@
|
||||||
source: crates/ruff/src/rules/pyflakes/mod.rs
|
source: crates/ruff/src/rules/pyflakes/mod.rs
|
||||||
expression: diagnostics
|
expression: diagnostics
|
||||||
---
|
---
|
||||||
|
- kind:
|
||||||
|
UndefinedName:
|
||||||
|
name: make
|
||||||
|
location:
|
||||||
|
row: 18
|
||||||
|
column: 18
|
||||||
|
end_location:
|
||||||
|
row: 18
|
||||||
|
column: 22
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
UndefinedName:
|
||||||
|
name: model
|
||||||
|
location:
|
||||||
|
row: 18
|
||||||
|
column: 24
|
||||||
|
end_location:
|
||||||
|
row: 18
|
||||||
|
column: 29
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
UndefinedName:
|
UndefinedName:
|
||||||
name: captured
|
name: captured
|
||||||
location:
|
location:
|
||||||
row: 22
|
row: 26
|
||||||
column: 19
|
column: 19
|
||||||
end_location:
|
end_location:
|
||||||
row: 22
|
row: 26
|
||||||
column: 27
|
column: 27
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
|
||||||
|
|
@ -194,6 +194,35 @@ expression: diagnostics
|
||||||
column: 13
|
column: 13
|
||||||
end_location:
|
end_location:
|
||||||
row: 122
|
row: 122
|
||||||
|
column: 22
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
UnusedVariable:
|
||||||
|
name: z
|
||||||
|
location:
|
||||||
|
row: 123
|
||||||
|
column: 12
|
||||||
|
end_location:
|
||||||
|
row: 123
|
||||||
|
column: 13
|
||||||
|
fix:
|
||||||
|
content: pass
|
||||||
|
location:
|
||||||
|
row: 123
|
||||||
|
column: 12
|
||||||
|
end_location:
|
||||||
|
row: 123
|
||||||
|
column: 17
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
UnusedVariable:
|
||||||
|
name: y
|
||||||
|
location:
|
||||||
|
row: 124
|
||||||
|
column: 13
|
||||||
|
end_location:
|
||||||
|
row: 124
|
||||||
column: 14
|
column: 14
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
|
||||||
|
|
@ -230,6 +230,17 @@ expression: diagnostics
|
||||||
column: 13
|
column: 13
|
||||||
end_location:
|
end_location:
|
||||||
row: 122
|
row: 122
|
||||||
|
column: 22
|
||||||
|
fix: ~
|
||||||
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
UnusedVariable:
|
||||||
|
name: y
|
||||||
|
location:
|
||||||
|
row: 124
|
||||||
|
column: 13
|
||||||
|
end_location:
|
||||||
|
row: 124
|
||||||
column: 14
|
column: 14
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue