Avoid unstable formatting in ellipsis-only body with trailing comment (#8984)
## Summary
We should avoid inlining the ellipsis in:
```python
def h():
...
# bye
```
Just as we omit the ellipsis in:
```python
def h():
# bye
...
```
Closes https://github.com/astral-sh/ruff/issues/8905.
This commit is contained in:
@@ -68,6 +68,10 @@ with True:
|
||||
with True:
|
||||
... # comment
|
||||
|
||||
with True:
|
||||
...
|
||||
# comment
|
||||
|
||||
match x:
|
||||
case 1:
|
||||
...
|
||||
@@ -99,4 +103,4 @@ try:
|
||||
except:
|
||||
... # comment
|
||||
finally:
|
||||
... # comment
|
||||
... # comment
|
||||
|
||||
@@ -511,7 +511,9 @@ pub(crate) fn contains_only_an_ellipsis(body: &[Stmt], comments: &Comments) -> b
|
||||
let [node] = body else {
|
||||
return false;
|
||||
};
|
||||
value.is_ellipsis_literal_expr() && !comments.has_leading(node)
|
||||
value.is_ellipsis_literal_expr()
|
||||
&& !comments.has_leading(node)
|
||||
&& !comments.has_trailing_own_line(node)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
|
||||
@@ -74,6 +74,10 @@ with True:
|
||||
with True:
|
||||
... # comment
|
||||
|
||||
with True:
|
||||
...
|
||||
# comment
|
||||
|
||||
match x:
|
||||
case 1:
|
||||
...
|
||||
@@ -105,7 +109,8 @@ try:
|
||||
except:
|
||||
... # comment
|
||||
finally:
|
||||
... # comment```
|
||||
... # comment
|
||||
```
|
||||
|
||||
## Output
|
||||
```python
|
||||
@@ -163,6 +168,10 @@ with True:
|
||||
|
||||
with True: ... # comment
|
||||
|
||||
with True:
|
||||
...
|
||||
# comment
|
||||
|
||||
match x:
|
||||
case 1: ...
|
||||
case 2:
|
||||
|
||||
Reference in New Issue
Block a user