mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-31 07:37:38 +00:00
Make Parameters
an optional field on ExprLambda
(#6669)
## Summary If a lambda doesn't contain any parameters, or any parameter _tokens_ (like `*`), we can use `None` for the parameters. This feels like a better representation to me, since, e.g., what should the `TextRange` be for a non-existent set of parameters? It also allows us to remove several sites where we check if the `Parameters` is empty by seeing if it contains any arguments, so semantically, we're already trying to detect and model around this elsewhere. Changing this also fixes a number of issues with dangling comments in parameter-less lambdas, since those comments are now automatically marked as dangling on the lambda. (As-is, we were also doing something not-great whereby the lambda was responsible for formatting dangling comments on the parameters, which has been removed.) Closes https://github.com/astral-sh/ruff/issues/6646. Closes https://github.com/astral-sh/ruff/issues/6647. ## Test Plan `cargo test`
This commit is contained in:
parent
ea72d5feba
commit
6a5acde226
30 changed files with 517 additions and 412 deletions
|
@ -542,7 +542,7 @@ pub struct ExprUnaryOp<'a> {
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprLambda<'a> {
|
||||
parameters: ComparableParameters<'a>,
|
||||
parameters: Option<ComparableParameters<'a>>,
|
||||
body: Box<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
|
@ -773,7 +773,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
|
|||
body,
|
||||
range: _,
|
||||
}) => Self::Lambda(ExprLambda {
|
||||
parameters: (parameters.as_ref()).into(),
|
||||
parameters: parameters.as_ref().map(Into::into),
|
||||
body: body.into(),
|
||||
}),
|
||||
ast::Expr::IfExp(ast::ExprIfExp {
|
||||
|
|
|
@ -2038,7 +2038,9 @@ impl AstNode for ast::ExprLambda {
|
|||
range: _,
|
||||
} = self;
|
||||
|
||||
visitor.visit_parameters(parameters);
|
||||
if let Some(parameters) = parameters {
|
||||
visitor.visit_parameters(parameters);
|
||||
}
|
||||
visitor.visit_expr(body);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -632,7 +632,7 @@ impl From<ExprUnaryOp> for Expr {
|
|||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ExprLambda {
|
||||
pub range: TextRange,
|
||||
pub parameters: Box<Parameters>,
|
||||
pub parameters: Option<Box<Parameters>>,
|
||||
pub body: Box<Expr>,
|
||||
}
|
||||
|
||||
|
|
|
@ -354,7 +354,9 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
|||
body,
|
||||
range: _,
|
||||
}) => {
|
||||
visitor.visit_parameters(parameters);
|
||||
if let Some(parameters) = parameters {
|
||||
visitor.visit_parameters(parameters);
|
||||
}
|
||||
visitor.visit_expr(body);
|
||||
}
|
||||
Expr::IfExp(ast::ExprIfExp {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue