diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 28397b6dcf..76449285be 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -871,7 +871,20 @@ fn handle_parameter_comment<'a>( CommentPlacement::Default(comment) } } else if comment.start() < parameter.name.start() { - CommentPlacement::leading(parameter, comment) + // For lambdas, where the parameters cannot be parenthesized and the first parameter thus + // starts at the same position as the parent parameters, mark a comment before the first + // parameter as leading on the parameters rather than the individual parameter to prevent + // the whole parameter list from breaking. + // + // Note that this check is not needed above because lambda parameters cannot have + // annotations. + if let Some(AnyNodeRef::Parameters(parameters)) = comment.enclosing_parent() + && parameters.start() == parameter.start() + { + CommentPlacement::leading(parameters, comment) + } else { + CommentPlacement::leading(parameter, comment) + } } else { CommentPlacement::Default(comment) } @@ -1835,10 +1848,8 @@ fn handle_lambda_comment<'a>( // ) // ``` if comment.start() < parameters.start() { - return if let Some(first) = parameters.iter().next() - && comment.line_position().is_own_line() - { - CommentPlacement::leading(first.as_parameter(), comment) + return if comment.line_position().is_own_line() { + CommentPlacement::leading(parameters, comment) } else { CommentPlacement::dangling(comment.enclosing_node(), comment) }; diff --git a/crates/ruff_python_formatter/src/expression/expr_lambda.rs b/crates/ruff_python_formatter/src/expression/expr_lambda.rs index 883ef3fdc5..cb32e79501 100644 --- a/crates/ruff_python_formatter/src/expression/expr_lambda.rs +++ b/crates/ruff_python_formatter/src/expression/expr_lambda.rs @@ -42,8 +42,8 @@ impl FormatNodeRule for FormatExprLambda { .split_at(dangling.partition_point(|comment| comment.end() < parameters.start())); if dangling_before_parameters.is_empty() { - // If the first parameter has a leading comment, insert a hard line break. This - // comment is associated as a leading comment on the first parameter: + // If the parameters have a leading comment, insert a hard line break. This + // comment is associated as a leading comment on the parameters: // // ```py // ( @@ -96,11 +96,7 @@ impl FormatNodeRule for FormatExprLambda { // *x: x // ) // ``` - if parameters - .iter() - .next() - .is_some_and(|parameter| comments.has_leading(parameter.as_parameter())) - { + if comments.has_leading(&**parameters) { hard_line_break().fmt(f)?; } else { write!(f, [space()])?; diff --git a/crates/ruff_python_formatter/src/other/parameters.rs b/crates/ruff_python_formatter/src/other/parameters.rs index 2e12f48456..1c6682bab1 100644 --- a/crates/ruff_python_formatter/src/other/parameters.rs +++ b/crates/ruff_python_formatter/src/other/parameters.rs @@ -241,32 +241,6 @@ impl FormatNodeRule for FormatParameters { let num_parameters = item.len(); if self.parentheses == ParametersParentheses::Never { - // In a lambda, format any leading comments on the first parameter outside of the - // parameters group so that the parameters don't break. For example, format this input: - // - // ```py - // ( - // lambda - // * # this becomes a leading comment on *x - // x, **y: - // x - // ) - // ``` - // - // as: - // - // ```py - // ( - // lambda - // # this becomes a leading comment on *x - // *x, **y: x - // ) - // ``` - if let Some(first) = item.iter().next() - && comments.has_leading(first.as_parameter()) - { - leading_node_comments(first.as_parameter()).fmt(f)?; - } write!(f, [group(&format_inner), dangling_comments(dangling)]) } else if num_parameters == 0 { let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);