diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 234752ba53..d0906d24b6 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -1865,7 +1865,7 @@ fn handle_unary_op_comment<'a>( .find(|token| token.kind == SimpleTokenKind::LParen) .map_or(unary_op.operand.start(), |lparen| lparen.start()); if comment.end() < up_to { - CommentPlacement::leading(unary_op, comment) + CommentPlacement::leading(&*unary_op.operand, comment) } else { CommentPlacement::Default(comment) } diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__unary.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__unary.py.snap index 67f54f9da9..2cdde4e0d9 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__unary.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__unary.py.snap @@ -1,7 +1,6 @@ --- source: crates/ruff_python_formatter/tests/fixtures.rs input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/unary.py -snapshot_kind: text --- ## Input ```python @@ -200,6 +199,11 @@ def foo(): not (aaaaaaaaaaaaaaaaaaaaa[bbbbbbbb, ccccccc]) and dddddddddd < eeeeeeeeeeeeeee ): pass + +# Regression test for https://github.com/astral-sh/ruff/issues/19226 +if '' and (not # +0): + pass ``` ## Output @@ -250,31 +254,35 @@ if +( pass if ( + not # comment - not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( + ~ # comment - ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( + - # comment - -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( + + # comment - +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass @@ -283,8 +291,8 @@ if ( if ( # unary comment - # operand comment - not ( + not # operand comment + ( # comment aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb @@ -318,31 +326,36 @@ if ( ## Trailing operator comments -if ( # comment - not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +if ( + not + # comment + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( + ~ # comment - ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( + - # comment - -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( + + # comment - +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass @@ -362,13 +375,16 @@ if ( pass if ( + not # comment - not a + a ): pass -if ( # comment - not a +if ( + not + # comment + a ): pass @@ -385,9 +401,9 @@ if True: # Regression test for: https://github.com/astral-sh/ruff/issues/7448 x = ( # a - # b + not # b # c - not ( # d + ( # d # e True ) @@ -415,4 +431,13 @@ def foo(): not (aaaaaaaaaaaaaaaaaaaaa[bbbbbbbb, ccccccc]) and dddddddddd < eeeeeeeeeeeeeee ): pass + + +# Regression test for https://github.com/astral-sh/ruff/issues/19226 +if "" and ( + not + # + 0 +): + pass ``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@parentheses__expression_parentheses_comments.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@parentheses__expression_parentheses_comments.py.snap index 5e8e826231..4a145d5e70 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@parentheses__expression_parentheses_comments.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@parentheses__expression_parentheses_comments.py.snap @@ -1,7 +1,6 @@ --- source: crates/ruff_python_formatter/tests/fixtures.rs input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/parentheses/expression_parentheses_comments.py -snapshot_kind: text --- ## Input ```python @@ -179,13 +178,13 @@ nested_parentheses4 = [ x = ( # unary comment - # in-between comment - not ( + not # in-between comment + ( # leading inner "a" ), - # in-between comment - not ( + not # in-between comment + ( # leading inner "b" ), @@ -194,8 +193,8 @@ x = ( "c" ), # 1 - # 2 - not ( # 3 + not # 2 + ( # 3 # 4 "d" ), @@ -203,8 +202,8 @@ x = ( if ( # unary comment - # in-between comment - not ( + not # in-between comment + ( # leading inner 1 )