diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py index 4e63cf11db..85038eb74e 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py @@ -102,3 +102,6 @@ aaaaaaaaaaaaaaaaaaaaa = [ c # negative decimal ] +# Parenthesized targets and iterators. +[x for (x) in y] +[x for x in (y)] diff --git a/crates/ruff_python_formatter/src/other/comprehension.rs b/crates/ruff_python_formatter/src/other/comprehension.rs index 82e4b4e119..423c4f1348 100644 --- a/crates/ruff_python_formatter/src/other/comprehension.rs +++ b/crates/ruff_python_formatter/src/other/comprehension.rs @@ -1,6 +1,6 @@ -use ruff_formatter::{format_args, write, Buffer, FormatError, FormatResult}; +use ruff_formatter::{format_args, write, Buffer, FormatResult}; use ruff_python_ast::{Comprehension, Expr}; -use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer}; +use ruff_python_trivia::{find_only_token_in_range, SimpleTokenKind}; use ruff_text_size::{Ranged, TextRange}; use crate::comments::{leading_comments, trailing_comments, SourceComment}; @@ -42,27 +42,14 @@ impl FormatNodeRule for FormatComprehension { dangling_item_comments.partition_point(|comment| comment.end() < target.start()), ); - let maybe_in_token = SimpleTokenizer::new( - f.context().source(), + let in_token = find_only_token_in_range( TextRange::new(target.end(), iter.start()), - ) - .skip_trivia() - .next(); - - let Some( - in_keyword @ SimpleToken { - kind: SimpleTokenKind::In, - .. - }, - ) = maybe_in_token - else { - return Err(FormatError::syntax_error( - "Expected `in` keyword between the `target` and `iter`.", - )); - }; + SimpleTokenKind::In, + f.context().source(), + ); let (before_in_comments, dangling_comments) = dangling_comments.split_at( - dangling_comments.partition_point(|comment| comment.end() < in_keyword.start()), + dangling_comments.partition_point(|comment| comment.end() < in_token.start()), ); let (trailing_in_comments, dangling_if_comments) = dangling_comments diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap index dcfd484622..a7007f72e4 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap @@ -108,6 +108,9 @@ aaaaaaaaaaaaaaaaaaaaa = [ c # negative decimal ] +# Parenthesized targets and iterators. +[x for (x) in y] +[x for x in (y)] ``` ## Output @@ -247,6 +250,10 @@ aaaaaaaaaaaaaaaaaaaaa = [ for components in b # pylint: disable=undefined-loop-variable # integer 1 may only have decimal 01-09 + c # negative decimal ] + +# Parenthesized targets and iterators. +[x for (x) in y] +[x for x in (y)] ```