Skip to content

handle_parenthesized_comment can't deal with certain kinds of match comments  #6866

@konstin

Description

@konstin

The following code is valid python, but crashes handle_parenthesized_comment:

match a:
    case A(
        # a
        b # b
        = # c
        2 # d
        # e
    ):
        pass
thread 'main' panicked at 'Unexpected token between nodes: `"\n        b # b\n        = # c\n        "`', crates/ruff_python_formatter/src/comments/placement.rs:147:17

The assert in question:

if tokenizer
.skip_trivia()
.take_while(|token| {
!matches!(
token.kind,
SimpleTokenKind::As | SimpleTokenKind::Def | SimpleTokenKind::Class
)
})
.any(|token| {
debug_assert!(
!matches!(token.kind, SimpleTokenKind::Bogus),
"Unexpected token between nodes: `{:?}`",
locator.slice(TextRange::new(comment.end(), following.start()))
);
token.kind() == SimpleTokenKind::RParen
})

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingformatterRelated to the formatter

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions