Skip to content

Comments

[refurb] Do not emit diagnostic when loop variables are used outside loop body (FURB122)#15757

Merged
AlexWaygood merged 8 commits intoastral-sh:mainfrom
InSyncWithFoo:FURB122
Jan 28, 2025
Merged

[refurb] Do not emit diagnostic when loop variables are used outside loop body (FURB122)#15757
AlexWaygood merged 8 commits intoastral-sh:mainfrom
InSyncWithFoo:FURB122

Conversation

@InSyncWithFoo
Copy link
Contributor

@InSyncWithFoo InSyncWithFoo commented Jan 27, 2025

Summary

Resolves #15725.

Previously, FURB122 analyzes for loops only at AST level, resulting in false positives, as shown in the linked issue.

With this change, now FURB122 will also analyze for loops at binding level. If a loop variable is used outside of the loop or shadows another variable, no diagnostic will be emitted.

Two-step analysis is necessary since it's possible for for loops not to have any bindings:

with open('file.txt', 'w') as f:
	for () in range(10):
		f.write('line')

Test Plan

cargo nextest run and cargo insta test.

@InSyncWithFoo
Copy link
Contributor Author

As the rule is now binding-sensitive, existing tests need to be rescoped. This leads to a rather big change, which I have isolated to the first commit.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 27, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

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

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

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

- docs/exts/extra_files_with_substitutions.py:51:13: FURB122 [*] Use of `output_file.write` in a for loop

latchbio/latch (+0 -1 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:180:9: FURB122 [*] Use of `f.write` in a for loop

Changes by rule (1 rules affected)

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

@InSyncWithFoo InSyncWithFoo changed the title [refurb] Do not emit diagnostic when loop variables are used outside of the loop (FURB122) [refurb] Do not emit diagnostic when loop variables are used outside loop body (FURB122) Jan 27, 2025
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Not a full review yet, just a couple of style nitpicks :-)

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

This looks good, thanks. A couple more points below, but nothing major

@AlexWaygood AlexWaygood self-assigned this Jan 28, 2025
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Thanks!

@AlexWaygood AlexWaygood enabled auto-merge (squash) January 28, 2025 19:12
@AlexWaygood AlexWaygood merged commit 72a4d34 into astral-sh:main Jan 28, 2025
20 checks passed
@InSyncWithFoo InSyncWithFoo deleted the FURB122 branch January 28, 2025 21:12
dcreager added a commit that referenced this pull request Jan 29, 2025
* main:
  [red-knot] Extend instance-attribute tests (#15808)
  Fix formatter warning message for `flake8-quotes` option (#15788)
  [`flake8-bugbear`] Exempt `NewType` calls where the original type is immutable (`B008`) (#15765)
  Add missing config docstrings (#15803)
  [`refurb`] Do not emit diagnostic when loop variables are used outside loop body (`FURB122`) (#15757)
  [`ruff`] Check for shadowed `map` before suggesting fix (`RUF058`) (#15790)
  [red-knot] Do not use explicit `knot_extensions.Unknown` declaration (#15787)
  Preserve quotes in generated byte strings (#15778)
  [minor] Simplify some `ExprStringLiteral` creation logic (#15775)
  Preserve quote style in generated code (#15726)
  Rename internal helper functions (#15771)
  [`airflow`] Extend airflow context parameter check for `BaseOperator.execute` (`AIR302`) (#15713)
  Implement tab autocomplete for `ruff config` (#15603)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FURB122 false positive when the target variable is used beyond the write call

2 participants