Repository: astral-sh/ruff
File: crates/ruff_python_formatter/src/comments/placement.rs (Rust)

Code before:
    if !preceding.is_statement() || !following.is_statement() {
        return CommentPlacement::Default(comment);
    }

    if comment.line_position().is_end_of_line() {
        return CommentPlacement::Default(comment);
    }

    // If the comment is directly attached to the following statement; make it a leading
    // comment:
    // ```python
    // x = 1
    //
    // # leading comment
    // y = 2
    // ```
    //
    // Otherwise, if there's at least one empty line, make it a trailing comment:
    // ```python
    // x = 1
    // # trailing comment
    //
    // y = 2
    // ```
    if max_empty_lines(&source[TextRange::new(comment.end(), following.start())]) == 0 {
        for token in SimpleTokenizer::new(source, TextRange::new(comment.end(), following.start()))
        {
            match token.kind() {
                SimpleTokenKind::Else | SimpleTokenKind::Finally => {
                    return CommentPlacement::dangling(comment.enclosing_node(), comment);
                }
                _ => {}
            }
        }
        CommentPlacement::leading(following, comment)
    } else {
        CommentPlacement::trailing(preceding, comment)
    }
}

/// Handles own line comments between two branches of a node.
/// ```python
/// for x in y:
///     pass
/// # This one ...
/// else:
///     print("I have no comments")
/// # ... but not this one
/// ```
fn handle_own_line_comment_between_branches<'a>(
    comment: DecoratedComment<'a>,

Review comment (suggestion):
Can you say more why we're handling this case here? I'd expect this to be handled by `handle_own_line_comment_between_branches` 

Code after:
    if !preceding.is_statement() || !following.is_statement() {
        return CommentPlacement::Default(comment);
    }

    if comment.line_position().is_end_of_line() {
        return CommentPlacement::Default(comment);
    }

    // If the comment is directly attached to the following statement; make it a leading
    // comment:
    // ```python
    // x = 1
    //
    // # leading comment
    // y = 2
    // ```
    //
    // Otherwise, if there's at least one empty line, make it a trailing comment:
    // ```python
    // x = 1
    // # trailing comment
    //
    // y = 2
    // ```
    if max_empty_lines(&source[TextRange::new(comment.end(), following.start())]) == 0 {
        CommentPlacement::leading(following, comment)
    } else {
        CommentPlacement::trailing(preceding, comment)
    }
}

/// Handles own line comments between two branches of a node.
/// ```python
/// for x in y:
///     pass
/// # This one ...
/// else:
///     print("I have no comments")
/// # ... but not this one
/// ```
fn handle_own_line_comment_between_branches<'a>(
    comment: DecoratedComment<'a>,
    preceding: AnyNodeRef<'a>,
    source: &str,
) -> CommentPlacement<'a> {
    // The following statement must be the first statement in an alternate body, otherwise check
    // if it's a comment after the final body and handle that case
    let Some(following) = comment.following_node() else {
        return CommentPlacement::Default(comment);
    };
    if !following.is_first_statement_in_alternate_body(comment.enclosing_node()) {