Skip to content

Commit 71881f0

Browse files
committed
pyupgrade: tighten diagnostic range for UP024 tuples (#19696
1 parent 8d4d782 commit 71881f0

4 files changed

Lines changed: 30 additions & 30 deletions

File tree

crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ fn atom_diagnostic(checker: &Checker, target: &Expr) {
9393

9494
/// Create a [`Diagnostic`] for a tuple of expressions.
9595
fn tuple_diagnostic(checker: &Checker, tuple: &ast::ExprTuple, aliases: &[&Expr]) {
96-
let mut diagnostic = checker.report_diagnostic(OSErrorAlias { name: None }, tuple.range());
96+
let Some(first_alias) = aliases.first() else {
97+
return;
98+
};
99+
let mut diagnostic =
100+
checker.report_diagnostic(OSErrorAlias { name: None }, first_alias.range());
97101
let semantic = checker.semantic();
98102
if semantic.has_builtin_binding("OSError") {
99103
// Filter out any `OSErrors` aliases.

crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP024_0.py.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ help: Replace `error` with builtin `OSError`
135135
39 | # Should NOT be in parentheses when replaced
136136

137137
UP024 [*] Replace aliased errors with `OSError`
138-
--> UP024_0.py:43:8
138+
--> UP024_0.py:43:9
139139
|
140140
41 | try:
141141
42 | pass
142142
43 | except (IOError,):
143-
| ^^^^^^^^^^
143+
| ^^^^^^^
144144
44 | pass
145145
45 | try:
146146
|
@@ -155,12 +155,12 @@ help: Replace with builtin `OSError`
155155
46 | pass
156156

157157
UP024 [*] Replace aliased errors with `OSError`
158-
--> UP024_0.py:47:8
158+
--> UP024_0.py:47:9
159159
|
160160
45 | try:
161161
46 | pass
162162
47 | except (mmap.error,):
163-
| ^^^^^^^^^^^^^
163+
| ^^^^^^^^^^
164164
48 | pass
165165
49 | try:
166166
|
@@ -175,12 +175,12 @@ help: Replace with builtin `OSError`
175175
50 | pass
176176

177177
UP024 [*] Replace aliased errors with `OSError`
178-
--> UP024_0.py:51:8
178+
--> UP024_0.py:51:9
179179
|
180180
49 | try:
181181
50 | pass
182182
51 | except (EnvironmentError, IOError, OSError, select.error):
183-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
183+
| ^^^^^^^^^^^^^^^^
184184
52 | pass
185185
|
186186
help: Replace with builtin `OSError`
@@ -194,12 +194,12 @@ help: Replace with builtin `OSError`
194194
54 | # Should be kept in parentheses (because multiple)
195195

196196
UP024 [*] Replace aliased errors with `OSError`
197-
--> UP024_0.py:58:8
197+
--> UP024_0.py:58:9
198198
|
199199
56 | try:
200200
57 | pass
201201
58 | except (IOError, KeyError, OSError):
202-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
202+
| ^^^^^^^
203203
59 | pass
204204
|
205205
help: Replace with builtin `OSError`
@@ -213,12 +213,12 @@ help: Replace with builtin `OSError`
213213
61 | # First should change, second should not
214214

215215
UP024 [*] Replace aliased errors with `OSError`
216-
--> UP024_0.py:65:8
216+
--> UP024_0.py:65:9
217217
|
218218
63 | try:
219219
64 | pass
220220
65 | except (IOError, error):
221-
| ^^^^^^^^^^^^^^^^
221+
| ^^^^^^^
222222
66 | pass
223223
67 | # These should not change
224224
|
@@ -252,12 +252,12 @@ help: Replace `mmap.error` with builtin `OSError`
252252
90 | try:
253253

254254
UP024 [*] Replace aliased errors with `OSError`
255-
--> UP024_0.py:105:11
255+
--> UP024_0.py:105:12
256256
|
257257
103 | try:
258258
104 | mac_address = get_primary_mac_address()
259259
105 | except(IOError, OSError) as ex:
260-
| ^^^^^^^^^^^^^^^^^^
260+
| ^^^^^^^
261261
106 | msg = 'Unable to query URL to get Owner ID: {u}\n{e}'.format(u=owner_id_url, e=ex)
262262
|
263263
help: Replace with builtin `OSError`

crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP024_1.py.snap

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
33
---
44
UP024 [*] Replace aliased errors with `OSError`
5-
--> UP024_1.py:5:8
5+
--> UP024_1.py:5:18
66
|
77
3 | try:
88
4 | pass
99
5 | except (OSError, mmap.error, IOError):
10-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
| ^^^^^^^^^^
1111
6 | pass
1212
7 | except (OSError, socket.error, KeyError):
1313
|
@@ -22,12 +22,12 @@ help: Replace with builtin `OSError`
2222
8 | pass
2323

2424
UP024 [*] Replace aliased errors with `OSError`
25-
--> UP024_1.py:7:8
25+
--> UP024_1.py:7:18
2626
|
2727
5 | except (OSError, mmap.error, IOError):
2828
6 | pass
2929
7 | except (OSError, socket.error, KeyError):
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
| ^^^^^^^^^^^^
3131
8 | pass
3232
|
3333
help: Replace with builtin `OSError`
@@ -41,18 +41,14 @@ help: Replace with builtin `OSError`
4141
10 | try:
4242

4343
UP024 [*] Replace aliased errors with `OSError`
44-
--> UP024_1.py:12:8
44+
--> UP024_1.py:14:5
4545
|
46-
10 | try:
47-
11 | pass
48-
12 | except (
49-
| ________^
50-
13 | | OSError,
51-
14 | | select.error,
52-
15 | | IOError,
53-
16 | | ):
54-
| |_^
55-
17 | pass
46+
12 | except (
47+
13 | OSError,
48+
14 | select.error,
49+
| ^^^^^^^^^^^^
50+
15 | IOError,
51+
16 | ):
5652
|
5753
help: Replace with builtin `OSError`
5854
9 |

crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP024_4.py.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
33
---
44
UP024 [*] Replace aliased errors with `OSError`
5-
--> UP024_4.py:9:8
5+
--> UP024_4.py:9:9
66
|
77
7 | conn.ensure_connection(max_retries=2)
88
8 | conn._close()
99
9 | except (socket.error, exceptions.OperationalError):
10-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
| ^^^^^^^^^^^^
1111
10 | return HttpResponseServerError("cache: cannot connect to broker.")
1212
|
1313
help: Replace with builtin `OSError`

0 commit comments

Comments
 (0)