Skip to content

Commit 059e90a

Browse files
authored
[refurb] Make example error out-of-the-box (FURB122) (#19297)
## Summary Part of #18972 This PR makes [for-loop-writes (FURB122)](https://docs.astral.sh/ruff/rules/for-loop-writes/#for-loop-writes-furb122)'s example error out-of-the-box. I also had to re-name the second case's variables to get both to raise at the same time, I suspect because of limitations in ruff's current semantic model. New names subject to bikeshedding, I just went with the least effort `_b` for binary suffix. [Old example](https://play.ruff.rs/19e8e47a-8058-4013-aef5-e9b5eab65962) ```py with Path("file").open("w") as f: for line in lines: f.write(line) with Path("file").open("wb") as f: for line in lines: f.write(line.encode()) ``` [New example](https://play.ruff.rs/e96b00e5-3c63-47c3-996d-dace420dd711) ```py from pathlib import Path with Path("file").open("w") as f: for line in lines: f.write(line) with Path("file").open("wb") as f_b: for line_b in lines_b: f_b.write(line_b.encode()) ``` The "Use instead" section was also modified similarly. ## Test Plan <!-- How was it tested? --> N/A, no functionality/tests affected
1 parent a4562ac commit 059e90a

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,26 @@ use crate::rules::refurb::helpers::parenthesize_loop_iter_if_necessary;
1919
///
2020
/// ## Example
2121
/// ```python
22+
/// from pathlib import Path
23+
///
2224
/// with Path("file").open("w") as f:
2325
/// for line in lines:
2426
/// f.write(line)
2527
///
26-
/// with Path("file").open("wb") as f:
27-
/// for line in lines:
28-
/// f.write(line.encode())
28+
/// with Path("file").open("wb") as f_b:
29+
/// for line_b in lines_b:
30+
/// f_b.write(line_b.encode())
2931
/// ```
3032
///
3133
/// Use instead:
3234
/// ```python
35+
/// from pathlib import Path
36+
///
3337
/// with Path("file").open("w") as f:
3438
/// f.writelines(lines)
3539
///
36-
/// with Path("file").open("wb") as f:
37-
/// f.writelines(line.encode() for line in lines)
40+
/// with Path("file").open("wb") as f_b:
41+
/// f_b.writelines(line_b.encode() for line_b in lines_b)
3842
/// ```
3943
///
4044
/// ## Fix safety

0 commit comments

Comments
 (0)