Skip to content

Commit 4568c1a

Browse files
authored
Merge pull request rust-lang#3042 from topecongiro/issue-3040
Do not trim a block from expression if its condition will go multi-line
2 parents cd8549e + f70d139 commit 4568c1a

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

src/matches.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::{ast, ptr};
1919
use comment::{combine_strs_with_missing_comments, rewrite_comment};
2020
use config::{Config, ControlBraceStyle, IndentStyle};
2121
use expr::{
22-
format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line,
22+
format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line, rewrite_cond,
2323
rewrite_multiple_patterns, ExprType, RhsTactics,
2424
};
2525
use lists::{itemize_list, write_list, ListFormatting};
@@ -231,7 +231,7 @@ fn rewrite_match_arm(
231231
) -> Option<String> {
232232
let (missing_span, attrs_str) = if !arm.attrs.is_empty() {
233233
if contains_skip(&arm.attrs) {
234-
let (_, body) = flatten_arm_body(context, &arm.body);
234+
let (_, body) = flatten_arm_body(context, &arm.body, None);
235235
// `arm.span()` does not include trailing comma, add it manually.
236236
return Some(format!(
237237
"{}{}",
@@ -313,16 +313,27 @@ fn block_can_be_flattened<'a>(
313313
// (extend, body)
314314
// @extend: true if the arm body can be put next to `=>`
315315
// @body: flattened body, if the body is block with a single expression
316-
fn flatten_arm_body<'a>(context: &'a RewriteContext, body: &'a ast::Expr) -> (bool, &'a ast::Expr) {
316+
fn flatten_arm_body<'a>(
317+
context: &'a RewriteContext,
318+
body: &'a ast::Expr,
319+
opt_shape: Option<Shape>,
320+
) -> (bool, &'a ast::Expr) {
317321
let can_extend =
318322
|expr| !context.config.force_multiline_blocks() && can_flatten_block_around_this(expr);
319323

320324
if let Some(ref block) = block_can_be_flattened(context, body) {
321325
if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node {
322326
if let ast::ExprKind::Block(..) = expr.node {
323-
flatten_arm_body(context, expr)
327+
flatten_arm_body(context, expr, None)
324328
} else {
325-
(can_extend(expr), &*expr)
329+
let cond_becomes_muti_line = opt_shape
330+
.and_then(|shape| rewrite_cond(context, expr, shape))
331+
.map_or(false, |cond| cond.contains('\n'));
332+
if cond_becomes_muti_line {
333+
(false, &*body)
334+
} else {
335+
(can_extend(expr), &*expr)
336+
}
326337
}
327338
} else {
328339
(false, &*body)
@@ -341,7 +352,11 @@ fn rewrite_match_body(
341352
arrow_span: Span,
342353
is_last: bool,
343354
) -> Option<String> {
344-
let (extend, body) = flatten_arm_body(context, body);
355+
let (extend, body) = flatten_arm_body(
356+
context,
357+
body,
358+
shape.offset_left(extra_offset(pats_str, shape) + 4),
359+
);
345360
let (is_block, is_empty_block) = if let ast::ExprKind::Block(ref block, _) = body.node {
346361
(
347362
true,

tests/source/match.rs

+14
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,17 @@ fn issue_2377() {
507507
Tok::TypeOf if prec <= 16 => {}
508508
}
509509
}
510+
511+
// #3040
512+
fn issue_3040() {
513+
{
514+
match foo {
515+
DevtoolScriptControlMsg::WantsLiveNotifications(id, to_send) => {
516+
match documents.find_window(id) {
517+
Some(window) => devtools::handle_wants_live_notifications(window.upcast(), to_send),
518+
None => return warn!("Message sent to closed pipeline {}.", id),
519+
}
520+
}
521+
}
522+
}
523+
}

tests/target/match.rs

+16
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,19 @@ fn issue_2377() {
536536
Tok::TypeOf if prec <= 16 => {}
537537
}
538538
}
539+
540+
// #3040
541+
fn issue_3040() {
542+
{
543+
match foo {
544+
DevtoolScriptControlMsg::WantsLiveNotifications(id, to_send) => {
545+
match documents.find_window(id) {
546+
Some(window) => {
547+
devtools::handle_wants_live_notifications(window.upcast(), to_send)
548+
}
549+
None => return warn!("Message sent to closed pipeline {}.", id),
550+
}
551+
}
552+
}
553+
}
554+
}

0 commit comments

Comments
 (0)