Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/types/overrides.rs (Rust)

Code before:
                    functions.append(&mut member_functions);
                }
            }
            if functions.is_empty() {
                None
            } else {
                Some(functions)
            }
        }
        _ => None,
    }
}

fn check_post_init_signature<'db>(
    context: &InferContext<'db, '_>,
    configuration: OverrideRulesConfig,
    class: ClassType<'db>,
    member: &Member<'db>,
    definition: Definition<'db>,
    policy: CodeGeneratorKind<'db>,
) {
    let db = context.db();

    if configuration.check_invalid_dataclasses()
        && member.name == "__post_init__"
        && let Some((static_class, spec)) = class.static_class_literal(db)
    {
        let init_var_fields = static_class
            .fields(db, spec, policy)
            .iter()
            .filter(|(_, field)| {
                matches!(
                    field.kind,
                    FieldKind::Dataclass {
                        init_only: true,
                        ..
                    }
                )
            });

        let first_parameter = Parameter::positional_only(Some(Name::new_static("self")))
            .with_annotated_type(Type::instance(db, class));

        let following_parameters = init_var_fields.map(|(name, field)| {
            Parameter::positional_only(Some(name.clone())).with_annotated_type(field.declared_ty)
        });

        let parameters = Parameters::new(
            db,
            std::iter::once(first_parameter).chain(following_parameters),
        );

Review comment (suggestion):
The `let` makes it harder to do early return, but you could do:

```rust
if !configuration.check_invalid_dataclasses() || member.name != "__post_init__" {
    return;
}
let Some((static_class, spec)) = class.static_class_literal(db) else {
    return;
}

// now less indentation for the rest of it
```

Code after:
                    functions.append(&mut member_functions);
                }
            }
            if functions.is_empty() {
                None
            } else {
                Some(functions)
            }
        }
        _ => None,
    }
}

fn check_post_init_signature<'db>(
    context: &InferContext<'db, '_>,
    configuration: OverrideRulesConfig,
    class: ClassType<'db>,
    member: &Member<'db>,
    definition: Definition<'db>,
    policy: CodeGeneratorKind<'db>,
) {
    let db = context.db();

    if !configuration.check_invalid_dataclasses() {
        return;
    }
    if member.name != "__post_init__" {
        return;
    }
    let Some((static_class, spec)) = class.static_class_literal(db) else {
        return;
    };

    let init_var_fields = static_class
        .fields(db, spec, policy)
        .iter()
        .filter(|(_, field)| {
            matches!(
                field.kind,
                FieldKind::Dataclass {
                    init_only: true,
                    ..
                }
            )
        });

    let first_parameter = Parameter::positional_only(Some(Name::new_static("self")))
        .with_annotated_type(Type::instance(db, class));

    let following_parameters = init_var_fields.map(|(name, field)| {
        Parameter::positional_only(Some(name.clone())).with_annotated_type(field.declared_ty)