mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
Allow collapsed-ellipsis bodies in other statements (#8499)
## Summary Black and Ruff's preview styles now collapse statements like: ```python from contextlib import nullcontext ctx = nullcontext() with ctx: ... ``` Historically, we made an exception here for classes (https://github.com/astral-sh/ruff/pull/2837). This PR extends it to other statement kinds for consistency with the formatter. Closes https://github.com/astral-sh/ruff/issues/8496.
This commit is contained in:
parent
4170ef0508
commit
8c146bbf11
3 changed files with 12 additions and 3 deletions
|
@ -69,3 +69,5 @@ while 1:
|
||||||
#: E701:2:3
|
#: E701:2:3
|
||||||
a = \
|
a = \
|
||||||
5;
|
5;
|
||||||
|
#:
|
||||||
|
with x(y) as z: ...
|
||||||
|
|
|
@ -24,7 +24,7 @@ use ruff_source_file::Locator;
|
||||||
/// if foo == "blah":
|
/// if foo == "blah":
|
||||||
/// do_blah_thing()
|
/// do_blah_thing()
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
/// [PEP 8]: https://peps.python.org/pep-0008/#other-recommendations
|
/// [PEP 8]: https://peps.python.org/pep-0008/#other-recommendations
|
||||||
#[violation]
|
#[violation]
|
||||||
pub struct MultipleStatementsOnOneLineColon;
|
pub struct MultipleStatementsOnOneLineColon;
|
||||||
|
@ -206,12 +206,13 @@ pub(crate) fn compound_statements(
|
||||||
{
|
{
|
||||||
colon = Some((range.start(), range.end()));
|
colon = Some((range.start(), range.end()));
|
||||||
|
|
||||||
// Allow `class C: ...`-style definitions in stubs.
|
// Allow `class C: ...`-style definitions.
|
||||||
allow_ellipsis = class.is_some();
|
allow_ellipsis = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Tok::Semi => {
|
Tok::Semi => {
|
||||||
semi = Some((range.start(), range.end()));
|
semi = Some((range.start(), range.end()));
|
||||||
|
allow_ellipsis = false;
|
||||||
}
|
}
|
||||||
Tok::Comment(..) | Tok::Indent | Tok::Dedent | Tok::NonLogicalNewline => {}
|
Tok::Comment(..) | Tok::Indent | Tok::Dedent | Tok::NonLogicalNewline => {}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -223,6 +224,7 @@ pub(crate) fn compound_statements(
|
||||||
|
|
||||||
// Reset.
|
// Reset.
|
||||||
semi = None;
|
semi = None;
|
||||||
|
allow_ellipsis = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((start, end)) = colon {
|
if let Some((start, end)) = colon {
|
||||||
|
@ -245,6 +247,7 @@ pub(crate) fn compound_statements(
|
||||||
try_ = None;
|
try_ = None;
|
||||||
while_ = None;
|
while_ = None;
|
||||||
with = None;
|
with = None;
|
||||||
|
allow_ellipsis = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,6 +92,8 @@ E70.py:71:4: E703 [*] Statement ends with an unnecessary semicolon
|
||||||
70 | a = \
|
70 | a = \
|
||||||
71 | 5;
|
71 | 5;
|
||||||
| ^ E703
|
| ^ E703
|
||||||
|
72 | #:
|
||||||
|
73 | with x(y) as z: ...
|
||||||
|
|
|
|
||||||
= help: Remove unnecessary semicolon
|
= help: Remove unnecessary semicolon
|
||||||
|
|
||||||
|
@ -101,5 +103,7 @@ E70.py:71:4: E703 [*] Statement ends with an unnecessary semicolon
|
||||||
70 70 | a = \
|
70 70 | a = \
|
||||||
71 |- 5;
|
71 |- 5;
|
||||||
71 |+ 5
|
71 |+ 5
|
||||||
|
72 72 | #:
|
||||||
|
73 73 | with x(y) as z: ...
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue