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:
Charlie Marsh 2023-08-18 11:34:54 -04:00 committed by GitHub
parent ea72d5feba
commit 6a5acde226
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 517 additions and 412 deletions

View file

@ -1354,14 +1354,12 @@ NamedExpression: ast::Expr = {
};
LambdaDef: ast::Expr = {
<location:@L> "lambda" <location_args:@L> <p:ParameterList<UntypedParameter, StarUntypedParameter, StarUntypedParameter>?> <end_location_args:@R> ":" <body:Test<"all">> <end_location:@R> =>? {
p.as_ref().map(validate_arguments).transpose()?;
let p = p
.unwrap_or_else(|| ast::Parameters::empty((location_args..end_location_args).into()));
<location:@L> "lambda" <location_args:@L> <parameters:ParameterList<UntypedParameter, StarUntypedParameter, StarUntypedParameter>?> <end_location_args:@R> ":" <body:Test<"all">> <end_location:@R> =>? {
parameters.as_ref().map(validate_arguments).transpose()?;
Ok(ast::Expr::Lambda(
ast::ExprLambda {
parameters: Box::new(p),
parameters: parameters.map(Box::new),
body: Box::new(body),
range: (location..end_location).into()
}