diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index d0906d24b6..1f763563f0 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -1861,11 +1861,17 @@ fn handle_unary_op_comment<'a>( | SimpleTokenKind::Plus | SimpleTokenKind::Minus ))); - let up_to = tokenizer + let lparen_start = tokenizer .find(|token| token.kind == SimpleTokenKind::LParen) - .map_or(unary_op.operand.start(), |lparen| lparen.start()); - if comment.end() < up_to { - CommentPlacement::leading(&*unary_op.operand, comment) + .map(|lparen| lparen.start()); + let up_to = lparen_start.unwrap_or(unary_op.operand.start()); + if lparen_start.is_none() + && comment.end() < unary_op.operand.start() + && comment.line_position().is_end_of_line() + { + CommentPlacement::dangling(unary_op, comment) + } else if comment.end() < up_to { + CommentPlacement::leading(unary_op, 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 2cdde4e0d9..74ba326706 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 @@ -254,35 +254,31 @@ if +( pass if ( - not # comment - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( - ~ # comment - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( - - # comment - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( - + # comment - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass @@ -291,8 +287,8 @@ if ( if ( # unary comment - not # operand comment - ( + # operand comment + not ( # comment aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb @@ -327,35 +323,27 @@ if ( ## Trailing operator comments if ( - not - # comment - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # comment + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( - ~ - # comment - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # comment + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( - - - # comment - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # comment + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass if ( - + - # comment - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # comment + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ): pass @@ -375,17 +363,12 @@ if ( pass if ( - not # comment - a + not a ): pass -if ( - not - # comment - a -): +if not a: # comment pass # Regression test for: https://github.com/astral-sh/ruff/issues/7423 @@ -401,9 +384,9 @@ if True: # Regression test for: https://github.com/astral-sh/ruff/issues/7448 x = ( # a - not # b + # b # c - ( # d + not ( # d # e True ) @@ -435,9 +418,7 @@ def foo(): # Regression test for https://github.com/astral-sh/ruff/issues/19226 if "" and ( - not - # - 0 + 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 4a145d5e70..5e8e826231 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,6 +1,7 @@ --- 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 @@ -178,13 +179,13 @@ nested_parentheses4 = [ x = ( # unary comment - not # in-between comment - ( + # in-between comment + not ( # leading inner "a" ), - not # in-between comment - ( + # in-between comment + not ( # leading inner "b" ), @@ -193,8 +194,8 @@ x = ( "c" ), # 1 - not # 2 - ( # 3 + # 2 + not ( # 3 # 4 "d" ), @@ -202,8 +203,8 @@ x = ( if ( # unary comment - not # in-between comment - ( + # in-between comment + not ( # leading inner 1 )