Skip to content

[refurb] Implement for-loop-writes (FURB122)#10630

Merged
dylwil3 merged 6 commits intoastral-sh:mainfrom
alex-700:latyshev/furb122
Jan 16, 2025
Merged

[refurb] Implement for-loop-writes (FURB122)#10630
dylwil3 merged 6 commits intoastral-sh:mainfrom
alex-700:latyshev/furb122

Conversation

@alex-700
Copy link
Copy Markdown
Contributor

Summary

Implement for-loop-writes (FURB122) lint

Test Plan

cargo test

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 27, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+12 -0 violations, +0 -0 fixes in 6 projects; 49 projects unchanged)

apache/airflow (+4 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

+ dev/breeze/src/airflow_breeze/commands/setup_commands.py:147:21: FURB122 [*] Use of `destination_file.write` in a for loop
+ docs/exts/extra_files_with_substitutions.py:41:17: FURB122 [*] Use of `output_file.write` in a for loop
+ docs/exts/extra_files_with_substitutions.py:51:13: FURB122 [*] Use of `output_file.write` in a for loop
+ providers/src/airflow/providers/amazon/aws/transfers/dynamodb_to_s3.py:233:13: FURB122 [*] Use of `temp_file.write` in a for loop

apache/superset (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

+ tests/integration_tests/core_tests.py:443:13: FURB122 Use of `test_file.write` in a for loop

bokeh/bokeh (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

+ scripts/milestone.py:333:9: FURB122 [*] Use of `out.write` in a for loop

latchbio/latch (+2 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

+ src/latch_cli/services/get_params.py:178:9: FURB122 [*] Use of `f.write` in a for loop
+ src/latch_cli/services/get_params.py:180:9: FURB122 [*] Use of `f.write` in a for loop

zulip/zulip (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

+ tools/droplets/add_mentor.py:69:13: FURB122 [*] Use of `f.write` in a for loop
+ zerver/lib/test_helpers.py:572:13: FURB122 [*] Use of `f.write` in a for loop
+ zerver/lib/test_runner.py:306:17: FURB122 [*] Use of `f.write` in a for loop

astropy/astropy (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

+ astropy/samp/lockfile_helpers.py:46:5: FURB122 [*] Use of `lockfile.write` in a for loop

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
FURB122 12 12 0 0 0

Copy link
Copy Markdown
Contributor

@VascoSch92 VascoSch92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice 😊👍

@charliermarsh charliermarsh self-assigned this Mar 28, 2024
@charliermarsh charliermarsh added rule Implementing or modifying a lint rule preview Related to preview mode features labels Mar 28, 2024
@dylwil3 dylwil3 changed the title [refurb] Implement for-loop-writes (FURB122) lint [refurb] Implement for-loop-writes (FURB122) Jan 16, 2025
@dylwil3
Copy link
Copy Markdown
Collaborator

dylwil3 commented Jan 16, 2025

This is an excellent contribution! Thank you so much, and apologies for the long delay in resolving this PR.

@dylwil3 dylwil3 merged commit 177bf72 into astral-sh:main Jan 16, 2025
@alex-700 alex-700 deleted the latyshev/furb122 branch June 10, 2025 20:46
dylwil3 added a commit that referenced this pull request Jun 23, 2025
## Summary

Part of #15584

This adds a `Fix safety` section to [for-loop-writes
(FURB122)](https://docs.astral.sh/ruff/rules/for-loop-writes/#for-loop-writes-furb122).

The fix/lint was introduced in #10630
No reasoning is given on the unsafety in the PR/code.
The unsafety is determined here:

https://github.com/astral-sh/ruff/blob/ea812d0813faf6e0ed2d39354e85d08f9b857c90/crates/ruff_linter/src/rules/refurb/rules/for_loop_writes.rs#L200-L204
Unsafe fix demonstration:
[playground](https://play.ruff.rs/06592f33-10b9-4a77-b31e-0d3a98f402f4)
```py
with open("issue.txt", "w") as f:
    for i in range(10):
        # will be deleted
        f.write(str(i))
```

---------

Co-authored-by: Dylan <dylwil3@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants