From 059e90a98f770f4b75030478eb098ad79a34d388 Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Mon, 14 Jul 2025 09:24:16 -0700 Subject: [PATCH] [`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 N/A, no functionality/tests affected --- .../src/rules/refurb/rules/for_loop_writes.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/ruff_linter/src/rules/refurb/rules/for_loop_writes.rs b/crates/ruff_linter/src/rules/refurb/rules/for_loop_writes.rs index 58dadc3e90..8f20877817 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/for_loop_writes.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/for_loop_writes.rs @@ -19,22 +19,26 @@ use crate::rules::refurb::helpers::parenthesize_loop_iter_if_necessary; /// /// ## Example /// ```python +/// 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: -/// for line in lines: -/// f.write(line.encode()) +/// with Path("file").open("wb") as f_b: +/// for line_b in lines_b: +/// f_b.write(line_b.encode()) /// ``` /// /// Use instead: /// ```python +/// from pathlib import Path +/// /// with Path("file").open("w") as f: /// f.writelines(lines) /// -/// with Path("file").open("wb") as f: -/// f.writelines(line.encode() for line in lines) +/// with Path("file").open("wb") as f_b: +/// f_b.writelines(line_b.encode() for line_b in lines_b) /// ``` /// /// ## Fix safety