This commit is contained in:
Soham Kute 2025-11-16 05:25:56 +01:00 committed by GitHub
commit 9a7941a91e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -111,13 +111,24 @@ 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!(
(for_target, arg) => {
// Parenthesize an unparenthesized generator expression argument to preserve semantics,
// e.g. `s.add(c for c in x)` -> `s.update((c for c in x) for x in ...)`.
let arg_text = 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,
locator.slice(arg),
arg_text,
locator.slice(for_target),
parenthesize_loop_iter_if_necessary(for_stmt, checker, IterLocation::Comprehension),
),
)
}
};
let applicability = if checker.comment_ranges().intersects(for_stmt.range) {