mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-31 15:47:41 +00:00
Add dangling comment handling for lambda
expressions (#7493)
## Summary This PR adds dangling comment handling for `lambda` expressions. In short, comments around the `lambda` and the `:` are all considered dangling. Comments that come between the `lambda` and the `:` may be moved after the colon for simplicity (this is an odd position for a comment anyway), unless they also precede the lambda parameters, in which case they're formatted before the parameters. Closes https://github.com/astral-sh/ruff/issues/7470. ## Test Plan `cargo test` No change in similarity. Before: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1632 | | django | 0.99982 | 2760 | 37 | | transformers | 0.99957 | 2587 | 398 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99983 | 3496 | 18 | | warehouse | 0.99929 | 648 | 16 | | zulip | 0.99962 | 1437 | 22 | After: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1632 | | django | 0.99982 | 2760 | 37 | | transformers | 0.99957 | 2587 | 398 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99983 | 3496 | 18 | | warehouse | 0.99929 | 648 | 16 | | zulip | 0.99962 | 1437 | 22 |
This commit is contained in:
parent
e07670ad97
commit
4c4eceee36
5 changed files with 310 additions and 33 deletions
|
@ -158,7 +158,57 @@ lambda *x\
|
|||
x: x
|
||||
)
|
||||
|
||||
lambda: ( # comment
|
||||
x)
|
||||
|
||||
(
|
||||
lambda: # comment
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda:
|
||||
# comment
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda # comment
|
||||
:
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda
|
||||
# comment
|
||||
:
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda: # comment
|
||||
( # comment
|
||||
x
|
||||
)
|
||||
)
|
||||
|
||||
(
|
||||
lambda # 1
|
||||
# 2
|
||||
x # 3
|
||||
# 4
|
||||
: # 5
|
||||
# 6
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda
|
||||
x,
|
||||
# comment
|
||||
y:
|
||||
z
|
||||
)
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -220,8 +270,7 @@ lambda x: lambda y: lambda z: (
|
|||
# Trailing
|
||||
|
||||
a = (
|
||||
lambda:
|
||||
# Dangling
|
||||
lambda: # Dangling
|
||||
1
|
||||
)
|
||||
|
||||
|
@ -268,20 +317,18 @@ lambda a, /, c: a
|
|||
|
||||
# Dangling comments without parameters.
|
||||
(
|
||||
lambda:
|
||||
lambda: # 3
|
||||
None
|
||||
)
|
||||
|
||||
(
|
||||
lambda:
|
||||
# 3
|
||||
None
|
||||
)
|
||||
|
||||
(
|
||||
lambda:
|
||||
# 3
|
||||
None
|
||||
)
|
||||
|
||||
(
|
||||
lambda:
|
||||
# 1
|
||||
lambda: # 1
|
||||
# 2
|
||||
# 3
|
||||
# 4
|
||||
|
@ -289,30 +336,83 @@ lambda a, /, c: a
|
|||
)
|
||||
|
||||
(
|
||||
lambda # comment
|
||||
lambda
|
||||
# comment
|
||||
*x: x
|
||||
)
|
||||
|
||||
(
|
||||
lambda # comment 1
|
||||
lambda
|
||||
# comment 1
|
||||
# comment 2
|
||||
*x:
|
||||
*x:
|
||||
# comment 3
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda # comment 1
|
||||
lambda # comment 1
|
||||
# comment 2
|
||||
*x: x # comment 3
|
||||
*x: # comment 3
|
||||
x
|
||||
)
|
||||
|
||||
lambda *x: x
|
||||
|
||||
(
|
||||
lambda # comment
|
||||
lambda
|
||||
# comment
|
||||
*x: x
|
||||
)
|
||||
|
||||
lambda: ( # comment
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda: # comment
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda:
|
||||
# comment
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda: # comment
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda:
|
||||
# comment
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda: # comment
|
||||
( # comment
|
||||
x
|
||||
)
|
||||
)
|
||||
|
||||
(
|
||||
lambda # 1
|
||||
# 2
|
||||
x: # 3
|
||||
# 4
|
||||
# 5
|
||||
# 6
|
||||
x
|
||||
)
|
||||
|
||||
(
|
||||
lambda x,
|
||||
# comment
|
||||
y: z
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue