Skip to content

Commit 43e4598

Browse files
committed
simplify
1 parent ddcff38 commit 43e4598

File tree

5 files changed

+132
-36
lines changed

5 files changed

+132
-36
lines changed

crates/ruff_linter/src/checkers/ast/mod.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -860,23 +860,17 @@ impl SemanticSyntaxContext for Checker<'_> {
860860
}
861861

862862
fn is_bound_parameter(&self, name: &str) -> bool {
863-
for scope in self.semantic.current_scopes() {
864-
match scope.kind {
865-
ScopeKind::Class(_) => return false,
866-
ScopeKind::Function(ast::StmtFunctionDef { parameters, .. })
867-
| ScopeKind::Lambda(ast::ExprLambda {
868-
parameters: Some(parameters),
869-
..
870-
}) => return parameters.includes(name),
871-
ScopeKind::Lambda(_)
872-
| ScopeKind::Generator { .. }
873-
| ScopeKind::Module
874-
| ScopeKind::Type
875-
| ScopeKind::DunderClassCell => {}
863+
match self.semantic.current_scope().kind {
864+
ScopeKind::Function(ast::StmtFunctionDef { parameters, .. }) => {
865+
parameters.includes(name)
876866
}
867+
ScopeKind::Class(_)
868+
| ScopeKind::Lambda(_)
869+
| ScopeKind::Generator { .. }
870+
| ScopeKind::Module
871+
| ScopeKind::Type
872+
| ScopeKind::DunderClassCell => false,
877873
}
878-
879-
false
880874
}
881875
}
882876

crates/ruff_python_parser/src/semantic_errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,8 +2105,10 @@ pub trait SemanticSyntaxContext {
21052105

21062106
fn report_semantic_error(&self, error: SemanticSyntaxError);
21072107

2108+
/// Returns `true` if the visitor is inside a `for` or `while` loop.
21082109
fn in_loop_context(&self) -> bool;
21092110

2111+
/// Returns `true` if `name` is a bound parameter in the current function or lambda scope.
21102112
fn is_bound_parameter(&self, name: &str) -> bool;
21112113
}
21122114

crates/ty_python_semantic/resources/mdtest/diagnostics/semantic_syntax_errors.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,31 @@ a = None
386386

387387
def f(a):
388388
global a # error: [invalid-syntax]
389+
390+
def g(a):
391+
if True:
392+
global a # error: [invalid-syntax]
393+
394+
def h(a):
395+
def inner():
396+
global a
397+
398+
def i(a):
399+
try:
400+
global a # error: [invalid-syntax]
401+
except Exception:
402+
pass
403+
404+
def f(a):
405+
a = 1
406+
global a # error: [invalid-syntax]
407+
408+
def f(a):
409+
a = 1
410+
a = 2
411+
global a # error: [invalid-syntax]
412+
413+
def f(a):
414+
class Inner:
415+
global a # ok
389416
```

crates/ty_python_semantic/resources/mdtest/snapshots/semantic_syntax_erro…_-_Semantic_syntax_erro…_-_name_is_parameter_an…_(99bae53daf67ae6e).snap

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,37 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/semantic_syn
1212
## mdtest_snippet.py
1313

1414
```
15-
1 | a = None
16-
2 |
17-
3 | def f(a):
18-
4 | global a # error: [invalid-syntax]
15+
1 | a = None
16+
2 |
17+
3 | def f(a):
18+
4 | global a # error: [invalid-syntax]
19+
5 |
20+
6 | def g(a):
21+
7 | if True:
22+
8 | global a # error: [invalid-syntax]
23+
9 |
24+
10 | def h(a):
25+
11 | def inner():
26+
12 | global a
27+
13 |
28+
14 | def i(a):
29+
15 | try:
30+
16 | global a # error: [invalid-syntax]
31+
17 | except Exception:
32+
18 | pass
33+
19 |
34+
20 | def f(a):
35+
21 | a = 1
36+
22 | global a # error: [invalid-syntax]
37+
23 |
38+
24 | def f(a):
39+
25 | a = 1
40+
26 | a = 2
41+
27 | global a # error: [invalid-syntax]
42+
28 |
43+
29 | def f(a):
44+
30 | class Inner:
45+
31 | global a # ok
1946
```
2047

2148
# Diagnostics
@@ -27,6 +54,64 @@ error[invalid-syntax]: name `a` is parameter and global
2754
3 | def f(a):
2855
4 | global a # error: [invalid-syntax]
2956
| ^
57+
5 |
58+
6 | def g(a):
3059
|
3160
3261
```
62+
63+
```
64+
error[invalid-syntax]: name `a` is parameter and global
65+
--> src/mdtest_snippet.py:8:16
66+
|
67+
6 | def g(a):
68+
7 | if True:
69+
8 | global a # error: [invalid-syntax]
70+
| ^
71+
9 |
72+
10 | def h(a):
73+
|
74+
75+
```
76+
77+
```
78+
error[invalid-syntax]: name `a` is parameter and global
79+
--> src/mdtest_snippet.py:16:16
80+
|
81+
14 | def i(a):
82+
15 | try:
83+
16 | global a # error: [invalid-syntax]
84+
| ^
85+
17 | except Exception:
86+
18 | pass
87+
|
88+
89+
```
90+
91+
```
92+
error[invalid-syntax]: name `a` is parameter and global
93+
--> src/mdtest_snippet.py:22:12
94+
|
95+
20 | def f(a):
96+
21 | a = 1
97+
22 | global a # error: [invalid-syntax]
98+
| ^
99+
23 |
100+
24 | def f(a):
101+
|
102+
103+
```
104+
105+
```
106+
error[invalid-syntax]: name `a` is parameter and global
107+
--> src/mdtest_snippet.py:27:12
108+
|
109+
25 | a = 1
110+
26 | a = 2
111+
27 | global a # error: [invalid-syntax]
112+
| ^
113+
28 |
114+
29 | def f(a):
115+
|
116+
117+
```

crates/ty_python_semantic/src/semantic_index/builder.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,24 +2822,12 @@ impl SemanticSyntaxContext for SemanticIndexBuilder<'_, '_> {
28222822
fn in_loop_context(&self) -> bool {
28232823
self.current_scope_info().current_loop.is_some()
28242824
}
2825+
28252826
fn is_bound_parameter(&self, name: &str) -> bool {
2826-
for scope_info in self.scope_stack.iter().rev() {
2827-
let scope = &self.scopes[scope_info.file_scope_id];
2828-
match scope.node() {
2829-
NodeWithScopeKind::Function(func) => {
2830-
let parameters = &func.node(self.module).parameters;
2831-
return parameters.includes(name);
2832-
}
2833-
NodeWithScopeKind::Lambda(lambda) => {
2834-
if let Some(parameters) = lambda.node(self.module).parameters.as_ref() {
2835-
return parameters.includes(name);
2836-
}
2837-
}
2838-
NodeWithScopeKind::Class(_) => return false,
2839-
_ => {}
2840-
}
2841-
}
2842-
false
2827+
self.scopes[self.current_scope()]
2828+
.node()
2829+
.as_function()
2830+
.is_some_and(|func| func.node(self.module).parameters.includes(name))
28432831
}
28442832
}
28452833

0 commit comments

Comments
 (0)