Repository: astral-sh/ruff
File: crates/ruff_linter/src/rules/ruff/rules/mutable_class_default.rs (Rust)

Code before:
                    && is_mutable_expr(value, checker.semantic())
                    && !is_class_var_annotation(annotation, checker.semantic())
                    && !is_final_annotation(annotation, checker.semantic())
                    && !is_immutable_annotation(annotation, checker.semantic(), &[])
                {
                    if dataclass_kind(class_def, checker.semantic()).is_some() {
                        continue;
                    }

                    // Avoid, e.g., Pydantic and msgspec models, which end up copying defaults on instance creation.
                    if has_default_copy_semantics(class_def, checker.semantic()) {
                        return;
                    }

                    checker.report_diagnostic(MutableClassDefault, value.range());
                }
            }
            Stmt::Assign(ast::StmtAssign { value, targets, .. }) => {
                if !targets.iter().all(|target| {
                    is_special_attribute(target)
                        || target
                            .as_name_expr()
                            .is_some_and(|name| class_var_targets.contains(&name.id))
                }) && is_mutable_expr(value, checker.semantic())
                {
                    // The `_fields_` property of a `ctypes.Structure` base class has it's
                    // immutability enforced  by the base class itself which will throw an error if
                    // it's set a second time
                    // See: https://docs.python.org/3/library/ctypes.html#ctypes.Structure._fields_
                    if is_ctypes_structure_fields(class_def, checker.semantic(), targets) {
                        return;
                    }

                    // Avoid, e.g., Pydantic and msgspec models, which end up copying defaults on instance creation.
                    if has_default_copy_semantics(class_def, checker.semantic()) {
                        return;
                    }

                    checker.report_diagnostic(MutableClassDefault, value.range());
                }
            }
            _ => (),
        }
    }
}


Review comment (suggestion):
```suggestion
                    // The `_fields_` property of a `ctypes.Structure` base class has its
```

Code after:
                    && is_mutable_expr(value, checker.semantic())
                    && !is_class_var_annotation(annotation, checker.semantic())
                    && !is_final_annotation(annotation, checker.semantic())
                    && !is_immutable_annotation(annotation, checker.semantic(), &[])
                {
                    if dataclass_kind(class_def, checker.semantic()).is_some() {
                        continue;
                    }

                    // Avoid, e.g., Pydantic and msgspec models, which end up copying defaults on instance creation.
                    if has_default_copy_semantics(class_def, checker.semantic()) {
                        return;
                    }

                    checker.report_diagnostic(MutableClassDefault, value.range());
                }
            }
            Stmt::Assign(ast::StmtAssign { value, targets, .. }) => {
                if !targets.iter().all(|target| {
                    is_special_attribute(target)
                        || target
                            .as_name_expr()
                            .is_some_and(|name| class_var_targets.contains(&name.id))
                }) && is_mutable_expr(value, checker.semantic())
                {
                    // The `_fields_` property of a `ctypes.Structure` base class has its
                    // immutability enforced  by the base class itself which will throw an error if
                    // it's set a second time
                    // See: https://docs.python.org/3/library/ctypes.html#ctypes.Structure._fields_
                    if is_ctypes_structure_fields(class_def, checker.semantic(), targets) {
                        return;
                    }

                    // Avoid, e.g., Pydantic and msgspec models, which end up copying defaults on instance creation.
                    if has_default_copy_semantics(class_def, checker.semantic()) {
                        return;
                    }

                    checker.report_diagnostic(MutableClassDefault, value.range());
                }
            }
            _ => (),
        }
    }
}