ruff
923b4dd0 - Fix panic when formatting comments in unary expressions

Commit
28 days ago
Fix panic when formatting comments in unary expressions Summary -- This is a second attempt at fixing #19226 based on the feedback in https://github.com/astral-sh/ruff/pull/20494#issuecomment-3467920065. We currently mark the comment in an expression like this: ```py if '' and (not # 0): pass ``` as a leading comment on `not`, eventually causing a panic in `Operand::has_unparenthesized_leading_comments` because the end of the comment is greater than the start of the expression. https://github.com/astral-sh/ruff/blob/a1d9cb5830eca9b63e7fb529504fc536e99bca23/crates/ruff_python_formatter/src/expression/binary_like.rs#L843 This PR fixes the issue by instead making such a comment a dangling comment on the unary expression. In the third commit, I instead tried making the comment a leading comment on the operand, which also looks pretty reasonable to me. Making it a dangling comment seems more in line with the docs on `handle_unary_op_comments`, though. I also tried deleting the leading comment logic in favor of the new dangling logic in the fifth commit before reverting in the sixth. This looks okay to me too, but the current state of the PR seems like the least invasive fix. Test Plan -- A new, minimized test case based on the issue. I also checked that the original snippet from the report works now. Co-authored-by: Takayuki Maeda <takoyaki0316@gmail.com>
Author
Committer
Parents
Loading