diff --git a/crates/ruff_python_ast/src/comparable.rs b/crates/ruff_python_ast/src/comparable.rs index 4e08e98d1b..208b97d055 100644 --- a/crates/ruff_python_ast/src/comparable.rs +++ b/crates/ruff_python_ast/src/comparable.rs @@ -363,7 +363,7 @@ impl<'a> From<&'a ast::Number> for ComparableNumber<'a> { } } -#[derive(Debug, PartialEq, Eq, Hash)] +#[derive(Debug, Default, PartialEq, Eq, Hash)] pub struct ComparableArguments<'a> { args: Vec>, keywords: Vec>, @@ -1051,7 +1051,7 @@ pub struct StmtClassDef<'a> { decorator_list: Vec>, name: &'a str, type_params: Option>, - arguments: Option>, + arguments: ComparableArguments<'a>, body: Vec>, } @@ -1309,7 +1309,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> { range: _, }) => Self::ClassDef(StmtClassDef { name: name.as_str(), - arguments: arguments.as_ref().map(Into::into), + arguments: arguments.as_ref().map(Into::into).unwrap_or_default(), body: body.iter().map(Into::into).collect(), decorator_list: decorator_list.iter().map(Into::into).collect(), type_params: type_params.as_ref().map(Into::into), diff --git a/crates/ruff_python_formatter/tests/normalizer.rs b/crates/ruff_python_formatter/tests/normalizer.rs index 5aab798d69..68c15349dd 100644 --- a/crates/ruff_python_formatter/tests/normalizer.rs +++ b/crates/ruff_python_formatter/tests/normalizer.rs @@ -32,33 +32,20 @@ impl Normalizer { impl Transformer for Normalizer { fn visit_stmt(&self, stmt: &mut Stmt) { - match stmt { - Stmt::ClassDef(class_def) => { - // Treat `class C: ...` and `class C(): ...` equivalently. - if class_def - .arguments - .as_ref() - .is_some_and(|arguments| arguments.is_empty()) - { - class_def.arguments = None; - } - } - Stmt::Delete(delete) => { - // Treat `del a, b` and `del (a, b)` equivalently. - delete.targets = delete - .targets - .clone() - .into_iter() - .flat_map(|target| { - if let Expr::Tuple(tuple) = target { - Left(tuple.elts.into_iter()) - } else { - Right(std::iter::once(target)) - } - }) - .collect(); - } - _ => {} + if let Stmt::Delete(delete) = stmt { + // Treat `del a, b` and `del (a, b)` equivalently. + delete.targets = delete + .targets + .clone() + .into_iter() + .flat_map(|target| { + if let Expr::Tuple(tuple) = target { + Left(tuple.elts.into_iter()) + } else { + Right(std::iter::once(target)) + } + }) + .collect(); } transformer::walk_stmt(self, stmt);