mirror of
https://github.com/astral-sh/ruff
synced 2026-01-08 15:14:19 -05: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:
@@ -878,9 +878,11 @@ impl<'a> Generator<'a> {
|
||||
range: _,
|
||||
}) => {
|
||||
group_if!(precedence::LAMBDA, {
|
||||
let npos = parameters.args.len() + parameters.posonlyargs.len();
|
||||
self.p(if npos > 0 { "lambda " } else { "lambda" });
|
||||
self.unparse_parameters(parameters);
|
||||
self.p("lambda");
|
||||
if let Some(parameters) = parameters {
|
||||
self.p(" ");
|
||||
self.unparse_parameters(parameters);
|
||||
}
|
||||
self.p(": ");
|
||||
self.unparse_expr(body, precedence::LAMBDA);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user