Merge is_guaranteed_mutable_expr into is_mutable_expr#23072
Merge is_guaranteed_mutable_expr into is_mutable_expr#23072eureka928 wants to merge 4 commits intoastral-sh:mainfrom
is_guaranteed_mutable_expr into is_mutable_expr#23072Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF012 | 4 | 4 | 0 | 0 | 0 |
| D102 | 2 | 1 | 1 | 0 | 0 |
|
Hi @ntBre thanks for last PR |
|
Hi, thanks for the PRs! As Micha asked you a couple of weeks ago (#22600 (comment)), please give us some time before pinging us. I'm subscribed to all notifications on the repository, so I'll see your new PRs as they come in. |
ntBre
left a comment
There was a problem hiding this comment.
We cannot stabilize preview features outside of minor releases. Instead of removing preview features, we needed to add a new preview feature and use it in the rules mentioned in #20851 because this expands the scope of all of these rules. The changes to B006 need to be reverted, and we need to use a similar pattern to lines 117-121 in the other rules.
Fixes false negatives in B039, RUF008, RUF012, and RUF024 by extending `is_mutable_expr` to handle generator expressions, tuples containing mutable elements, and walrus (`:=`) expressions. These patterns were previously only detected in B006 behind a preview gate. Also stabilizes the B006 fix generation to always preserve original source text, which correctly handles walrus operator parentheses. Closes astral-sh#20851
The rebase added a new class C before the field() test class (now class D), shifting line numbers in both the stable and preview snapshots. Co-Authored-By: Claude Opus 4.5 <[email protected]>
The field(default=...) detection from astral-sh#23046 is behind a preview gate, so class D diagnostics should only appear in the preview snapshot. Co-Authored-By: Claude Opus 4.5 <[email protected]>
221c5f7 to
7a80bf9
Compare
…table_expr` Remove B006-specific preview flags (`is_b006_check_guaranteed_mutable_expr_enabled`, `is_b006_unsafe_fix_preserve_assignment_expr_enabled`) in favor of the shared `is_extended_mutable_expr_enabled` flag used by all other mutable-expression rules. Delete the duplicate local `is_guaranteed_mutable_expr` from B006 since it is identical to `is_mutable_expr_extended` in typing.rs.
Summary
Resolves #20851.
is_mutable_exprwas missing several expression types thatis_guaranteed_mutable_expr(used only by B006) already handled: generators, walrus/named expressions, and tuples containing mutable elements. This caused false negatives in B039, RUF008, RUF012, and RUF024.This PR merges those extra cases directly into
is_mutable_expr:Expr::Generator— generators are now recognized as mutableExpr::Named— walrus expressions (e.g.x := []) check the assigned value recursivelyExpr::Tuple— tuples are mutable if any element is mutableWith these patterns unified in
is_mutable_expr, the separateis_b006_check_guaranteed_mutable_expr_enabledandis_b006_unsafe_fix_preserve_assignment_expr_enabledpreview gates are no longer needed and are removed.Test Plan
cargo nextest run -p ruff_linterandcargo clippy.Added test fixtures for all four affected rules covering generators, walrus expressions, and tuples with mutable elements:
ContextVar("cv", default=(x for x in "cv")),(x := []),([],)dict.fromkeys()values