Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB142.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,11 @@ def g():

for x in (1,) if True else (2,):
s.add(x)

# https://github.com/astral-sh/ruff/issues/21098
for x in ("abc", "def"):
s.add(c for c in x)

# don't add extra parens for already parenthesized generators
for x in ("abc", "def"):
s.add((c for c in x))
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,21 @@ pub(crate) fn for_loop_set_mutations(checker: &Checker, for_stmt: &StmtFor) {
parenthesize_loop_iter_if_necessary(for_stmt, checker, IterLocation::Call),
)
}
(for_target, arg) => format!(
"{}.{batch_method_name}({} for {} in {})",
set.id,
locator.slice(arg),
locator.slice(for_target),
parenthesize_loop_iter_if_necessary(for_stmt, checker, IterLocation::Comprehension),
),
(for_target, arg) => {
let arg_content = match arg {
Expr::Generator(generator) if !generator.parenthesized => {
format!("({})", locator.slice(arg))
}
_ => locator.slice(arg).to_string(),
};
format!(
"{}.{batch_method_name}({} for {} in {})",
set.id,
arg_content,
locator.slice(for_target),
parenthesize_loop_iter_if_necessary(for_stmt, checker, IterLocation::Comprehension),
)
}
};

let applicability = if checker.comment_ranges().intersects(for_stmt.range) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ FURB142 [*] Use of `set.add()` in a for loop
100 | / for x in (1,) if True else (2,):
101 | | s.add(x)
| |____________^
102 |
103 | # https://github.com/astral-sh/ruff/issues/21098
|
help: Replace with `.update()`
97 | for x in lambda: 0:
Expand All @@ -394,3 +396,43 @@ help: Replace with `.update()`
- for x in (1,) if True else (2,):
- s.add(x)
100 + s.update((1,) if True else (2,))
101 |
102 | # https://github.com/astral-sh/ruff/issues/21098
103 | for x in ("abc", "def"):

FURB142 [*] Use of `set.add()` in a for loop
--> FURB142.py:104:1
|
103 | # https://github.com/astral-sh/ruff/issues/21098
104 | / for x in ("abc", "def"):
105 | | s.add(c for c in x)
| |_______________________^
106 |
107 | # don't add extra parens for already parenthesized generators
|
help: Replace with `.update()`
101 | s.add(x)
102 |
103 | # https://github.com/astral-sh/ruff/issues/21098
- for x in ("abc", "def"):
- s.add(c for c in x)
104 + s.update((c for c in x) for x in ("abc", "def"))
105 |
106 | # don't add extra parens for already parenthesized generators
107 | for x in ("abc", "def"):

FURB142 [*] Use of `set.add()` in a for loop
--> FURB142.py:108:1
|
107 | # don't add extra parens for already parenthesized generators
108 | / for x in ("abc", "def"):
109 | | s.add((c for c in x))
| |_________________________^
|
help: Replace with `.update()`
105 | s.add(c for c in x)
106 |
107 | # don't add extra parens for already parenthesized generators
- for x in ("abc", "def"):
- s.add((c for c in x))
108 + s.update((c for c in x) for x in ("abc", "def"))
Loading