[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
This commit is contained in:
GiGaGon 2025-07-14 09:24:16 -07:00 committed by GitHub
parent a4562ac673
commit 059e90a98f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -19,22 +19,26 @@ use crate::rules::refurb::helpers::parenthesize_loop_iter_if_necessary;
/// ///
/// ## Example /// ## Example
/// ```python /// ```python
/// from pathlib import Path
///
/// with Path("file").open("w") as f: /// with Path("file").open("w") as f:
/// for line in lines: /// for line in lines:
/// f.write(line) /// f.write(line)
/// ///
/// with Path("file").open("wb") as f: /// with Path("file").open("wb") as f_b:
/// for line in lines: /// for line_b in lines_b:
/// f.write(line.encode()) /// f_b.write(line_b.encode())
/// ``` /// ```
/// ///
/// Use instead: /// Use instead:
/// ```python /// ```python
/// from pathlib import Path
///
/// with Path("file").open("w") as f: /// with Path("file").open("w") as f:
/// f.writelines(lines) /// f.writelines(lines)
/// ///
/// with Path("file").open("wb") as f: /// with Path("file").open("wb") as f_b:
/// f.writelines(line.encode() for line in lines) /// f_b.writelines(line_b.encode() for line_b in lines_b)
/// ``` /// ```
/// ///
/// ## Fix safety /// ## Fix safety