Skip to content

FURB142 fix should parenthesize generator expressions #21098

@dscorbett

Description

@dscorbett

Summary

The fix for for-loop-set-mutations (FURB142) should parenthesize unparenthesized generator expressions to avoid changing the program’s behavior. Example:

$ cat >furb142.py <<'# EOF'
s = set()
for x in ("abc", "def"):
    s.add(c for c in x)
# EOF

$ python furb142.py; echo $?
0

$ ruff check --isolated furb142.py --select FURB142 --preview --fix
Found 1 error (1 fixed, 0 remaining).

$ python furb142.py 2>&1 | tail -n 3
    s.update(c for c in x for x in ("abc", "def"))
                        ^
NameError: name 'x' is not defined

Version

ruff 0.14.2 (83a3bc4 2025-10-23)

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfixesRelated to suggested fixes for violations

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions