Skip to content

Commit 981083c

Browse files
dylwil3ntBre
andcommitted
[refurb] Stabilize fix safety for readlines-in-for (FURB129) (#18496)
Note that the preview behavior was not documented (shame on us!) so the documentation was not modified. --------- Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
1 parent a2de81c commit 981083c

File tree

6 files changed

+24
-385
lines changed

6 files changed

+24
-385
lines changed

crates/ruff_linter/src/preview.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ pub(crate) const fn is_check_file_level_directives_enabled(settings: &LinterSett
126126
settings.preview.is_enabled()
127127
}
128128

129-
// https://github.com/astral-sh/ruff/pull/17644
130-
pub(crate) const fn is_readlines_in_for_fix_safe_enabled(settings: &LinterSettings) -> bool {
131-
settings.preview.is_enabled()
132-
}
133-
134-
pub(crate) const fn multiple_with_statements_fix_safe_enabled(settings: &LinterSettings) -> bool {
129+
// https://github.com/astral-sh/ruff/pull/18208
130+
pub(crate) const fn is_multiple_with_statements_fix_safe_enabled(
131+
settings: &LinterSettings,
132+
) -> bool {
135133
settings.preview.is_enabled()
136134
}

crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::fix_with;
1010
use crate::Fix;
1111
use crate::checkers::ast::Checker;
1212
use crate::fix::edits::fits;
13-
use crate::preview::multiple_with_statements_fix_safe_enabled;
13+
use crate::preview::is_multiple_with_statements_fix_safe_enabled;
1414
use crate::{FixAvailability, Violation};
1515

1616
/// ## What it does
@@ -195,7 +195,7 @@ pub(crate) fn multiple_with_statements(
195195
checker.settings.tab_size,
196196
)
197197
}) {
198-
if multiple_with_statements_fix_safe_enabled(checker.settings) {
198+
if is_multiple_with_statements_fix_safe_enabled(checker.settings) {
199199
Ok(Some(Fix::safe_edit(edit)))
200200
} else {
201201
Ok(Some(Fix::unsafe_edit(edit)))

crates/ruff_linter/src/rules/refurb/mod.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,6 @@ mod tests {
6262
Ok(())
6363
}
6464

65-
#[test_case(Rule::ReadlinesInFor, Path::new("FURB129.py"))]
66-
fn preview(rule_code: Rule, path: &Path) -> Result<()> {
67-
let snapshot = format!(
68-
"preview__{}_{}",
69-
rule_code.noqa_code(),
70-
path.to_string_lossy()
71-
);
72-
let diagnostics = test_path(
73-
Path::new("refurb").join(path).as_path(),
74-
&settings::LinterSettings {
75-
preview: settings::types::PreviewMode::Enabled,
76-
..settings::LinterSettings::for_rule(rule_code)
77-
},
78-
)?;
79-
assert_messages!(snapshot, diagnostics);
80-
Ok(())
81-
}
82-
8365
#[test]
8466
fn write_whole_file_python_39() -> Result<()> {
8567
let diagnostics = test_path(

crates/ruff_linter/src/rules/refurb/rules/readlines_in_for.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use ruff_text_size::Ranged;
77

88
use crate::checkers::ast::Checker;
99
use crate::fix::edits::pad_end;
10-
use crate::preview::is_readlines_in_for_fix_safe_enabled;
1110
use crate::{AlwaysFixableViolation, Edit, Fix};
1211

1312
/// ## What it does
@@ -106,9 +105,5 @@ fn readlines_in_iter(checker: &Checker, iter_expr: &Expr) {
106105
};
107106

108107
let mut diagnostic = checker.report_diagnostic(ReadlinesInFor, expr_call.range());
109-
diagnostic.set_fix(if is_readlines_in_for_fix_safe_enabled(checker.settings) {
110-
Fix::safe_edit(edit)
111-
} else {
112-
Fix::unsafe_edit(edit)
113-
});
108+
diagnostic.set_fix(Fix::safe_edit(edit));
114109
}

crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB129_FURB129.py.snap

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ FURB129.py:7:18: FURB129 [*] Instead of calling `readlines()`, iterate over file
1212
|
1313
= help: Remove `readlines()`
1414

15-
Unsafe fix
15+
Safe fix
1616
4 4 |
1717
5 5 | # Errors
1818
6 6 | with open("FURB129.py") as f:
@@ -33,7 +33,7 @@ FURB129.py:9:35: FURB129 [*] Instead of calling `readlines()`, iterate over file
3333
|
3434
= help: Remove `readlines()`
3535

36-
Unsafe fix
36+
Safe fix
3737
6 6 | with open("FURB129.py") as f:
3838
7 7 | for _line in f.readlines():
3939
8 8 | pass
@@ -53,7 +53,7 @@ FURB129.py:10:35: FURB129 [*] Instead of calling `readlines()`, iterate over fil
5353
|
5454
= help: Remove `readlines()`
5555

56-
Unsafe fix
56+
Safe fix
5757
7 7 | for _line in f.readlines():
5858
8 8 | pass
5959
9 9 | a = [line.lower() for line in f.readlines()]
@@ -74,7 +74,7 @@ FURB129.py:11:49: FURB129 [*] Instead of calling `readlines()`, iterate over fil
7474
|
7575
= help: Remove `readlines()`
7676

77-
Unsafe fix
77+
Safe fix
7878
8 8 | pass
7979
9 9 | a = [line.lower() for line in f.readlines()]
8080
10 10 | b = {line.upper() for line in f.readlines()}
@@ -93,7 +93,7 @@ FURB129.py:14:18: FURB129 [*] Instead of calling `readlines()`, iterate over fil
9393
|
9494
= help: Remove `readlines()`
9595

96-
Unsafe fix
96+
Safe fix
9797
11 11 | c = {line.lower(): line.upper() for line in f.readlines()}
9898
12 12 |
9999
13 13 | with Path("FURB129.py").open() as f:
@@ -113,7 +113,7 @@ FURB129.py:17:14: FURB129 [*] Instead of calling `readlines()`, iterate over fil
113113
|
114114
= help: Remove `readlines()`
115115

116-
Unsafe fix
116+
Safe fix
117117
14 14 | for _line in f.readlines():
118118
15 15 | pass
119119
16 16 |
@@ -133,7 +133,7 @@ FURB129.py:20:14: FURB129 [*] Instead of calling `readlines()`, iterate over fil
133133
|
134134
= help: Remove `readlines()`
135135

136-
Unsafe fix
136+
Safe fix
137137
17 17 | for _line in open("FURB129.py").readlines():
138138
18 18 | pass
139139
19 19 |
@@ -154,7 +154,7 @@ FURB129.py:26:18: FURB129 [*] Instead of calling `readlines()`, iterate over fil
154154
|
155155
= help: Remove `readlines()`
156156

157-
Unsafe fix
157+
Safe fix
158158
23 23 |
159159
24 24 | def func():
160160
25 25 | f = Path("FURB129.py").open()
@@ -173,7 +173,7 @@ FURB129.py:32:18: FURB129 [*] Instead of calling `readlines()`, iterate over fil
173173
|
174174
= help: Remove `readlines()`
175175

176-
Unsafe fix
176+
Safe fix
177177
29 29 |
178178
30 30 |
179179
31 31 | def func(f: io.BytesIO):
@@ -194,7 +194,7 @@ FURB129.py:38:22: FURB129 [*] Instead of calling `readlines()`, iterate over fil
194194
|
195195
= help: Remove `readlines()`
196196

197-
Unsafe fix
197+
Safe fix
198198
35 35 |
199199
36 36 | def func():
200200
37 37 | with (open("FURB129.py") as f, foo as bar):
@@ -213,7 +213,7 @@ FURB129.py:47:17: FURB129 [*] Instead of calling `readlines()`, iterate over fil
213213
|
214214
= help: Remove `readlines()`
215215

216-
Unsafe fix
216+
Safe fix
217217
44 44 | import builtins
218218
45 45 |
219219
46 46 | with builtins.open("FURB129.py") as f:
@@ -232,7 +232,7 @@ FURB129.py:54:17: FURB129 [*] Instead of calling `readlines()`, iterate over fil
232232
|
233233
= help: Remove `readlines()`
234234

235-
Unsafe fix
235+
Safe fix
236236
51 51 | from builtins import open as o
237237
52 52 |
238238
53 53 | with o("FURB129.py") as f:
@@ -252,7 +252,7 @@ FURB129.py:93:17: FURB129 [*] Instead of calling `readlines()`, iterate over fil
252252
|
253253
= help: Remove `readlines()`
254254

255-
Unsafe fix
255+
Safe fix
256256
90 90 |
257257
91 91 | # https://github.com/astral-sh/ruff/issues/18231
258258
92 92 | with open("furb129.py") as f:
@@ -270,7 +270,7 @@ FURB129.py:97:23: FURB129 [*] Instead of calling `readlines()`, iterate over fil
270270
|
271271
= help: Remove `readlines()`
272272

273-
Unsafe fix
273+
Safe fix
274274
94 94 | pass
275275
95 95 |
276276
96 96 | with open("furb129.py") as f:
@@ -290,7 +290,7 @@ FURB129.py:101:17: FURB129 [*] Instead of calling `readlines()`, iterate over fi
290290
|
291291
= help: Remove `readlines()`
292292

293-
Unsafe fix
293+
Safe fix
294294
98 98 |
295295
99 99 |
296296
100 100 | with open("furb129.py") as f:
@@ -310,7 +310,7 @@ FURB129.py:103:16: FURB129 [*] Instead of calling `readlines()`, iterate over fi
310310
|
311311
= help: Remove `readlines()`
312312

313-
Unsafe fix
313+
Safe fix
314314
100 100 | with open("furb129.py") as f:
315315
101 101 | for line in (((f))).readlines():
316316
102 102 | pass
@@ -328,7 +328,7 @@ FURB129.py:107:29: FURB129 [*] Instead of calling `readlines()`, iterate over fi
328328
|
329329
= help: Remove `readlines()`
330330

331-
Unsafe fix
331+
Safe fix
332332
104 104 | pass
333333
105 105 |
334334
106 106 | # Test case for issue #17683 (missing space before keyword)

0 commit comments

Comments
 (0)