diff --git a/src/expr.rs b/src/expr.rs index 15ffb842ee495..fbbc592e54722 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -801,6 +801,20 @@ impl<'a> ControlFlow<'a> { } } +/// Returns true if the last line of pat_str has leading whitespace and it is wider than the +/// shape's indent. +fn last_line_offsetted(start_column: usize, pat_str: &str) -> bool { + let mut leading_whitespaces = 0; + for c in pat_str.chars().rev() { + match c { + '\n' => break, + _ if c.is_whitespace() => leading_whitespaces += 1, + _ => leading_whitespaces = 0, + } + } + leading_whitespaces > start_column +} + impl<'a> ControlFlow<'a> { fn rewrite_pat_expr( &self, @@ -885,7 +899,8 @@ impl<'a> ControlFlow<'a> { .saturating_sub(constr_shape.used_width() + offset + brace_overhead); let force_newline_brace = (pat_expr_string.contains('\n') || pat_expr_string.len() > one_line_budget) - && !last_line_extendable(&pat_expr_string); + && (!last_line_extendable(&pat_expr_string) + || last_line_offsetted(shape.used_width(), &pat_expr_string)); // Try to format if-else on single line. if self.allow_single_line @@ -1977,3 +1992,29 @@ pub fn is_method_call(expr: &ast::Expr) -> bool { _ => false, } } + +#[cfg(test)] +mod test { + use super::last_line_offsetted; + + #[test] + fn test_last_line_offsetted() { + let lines = "one\n two"; + assert_eq!(last_line_offsetted(2, lines), true); + assert_eq!(last_line_offsetted(4, lines), false); + assert_eq!(last_line_offsetted(6, lines), false); + + let lines = "one two"; + assert_eq!(last_line_offsetted(2, lines), false); + assert_eq!(last_line_offsetted(0, lines), false); + + let lines = "\ntwo"; + assert_eq!(last_line_offsetted(2, lines), false); + assert_eq!(last_line_offsetted(0, lines), false); + + let lines = "one\n two three"; + assert_eq!(last_line_offsetted(2, lines), true); + let lines = "one\n two three"; + assert_eq!(last_line_offsetted(2, lines), false); + } +} diff --git a/tests/source/issue-3038.rs b/tests/source/issue-3038.rs new file mode 100644 index 0000000000000..0fbb05ddc0e62 --- /dev/null +++ b/tests/source/issue-3038.rs @@ -0,0 +1,20 @@ +impl HTMLTableElement { + fn func() { + if number_of_row_elements == 0 { + if let Some(last_tbody) = node.rev_children() + .filter_map(DomRoot::downcast::) + .find(|n| n.is::() && n.local_name() == &local_name!("tbody")) { + last_tbody.upcast::().AppendChild(new_row.upcast::()) + .expect("InsertRow failed to append first row."); + } + } + + if number_of_row_elements == 0 { + if let Some(last_tbody) = node + .find(|n| n.is::() && n.local_name() == &local_name!("tbody")) { + last_tbody.upcast::().AppendChild(new_row.upcast::()) + .expect("InsertRow failed to append first row."); + } + } + } +} diff --git a/tests/target/issue-2985.rs b/tests/target/issue-2985.rs index 37b216ea9b137..faad859236dc9 100644 --- a/tests/target/issue-2985.rs +++ b/tests/target/issue-2985.rs @@ -27,7 +27,8 @@ fn foo() { .map(String::as_ref) .unwrap_or("") .is_empty() - }) { + }) + { do_something(); } } diff --git a/tests/target/issue-3038.rs b/tests/target/issue-3038.rs new file mode 100644 index 0000000000000..3c398b825d7e7 --- /dev/null +++ b/tests/target/issue-3038.rs @@ -0,0 +1,29 @@ +impl HTMLTableElement { + fn func() { + if number_of_row_elements == 0 { + if let Some(last_tbody) = node + .rev_children() + .filter_map(DomRoot::downcast::) + .find(|n| { + n.is::() && n.local_name() == &local_name!("tbody") + }) + { + last_tbody + .upcast::() + .AppendChild(new_row.upcast::()) + .expect("InsertRow failed to append first row."); + } + } + + if number_of_row_elements == 0 { + if let Some(last_tbody) = node.find(|n| { + n.is::() && n.local_name() == &local_name!("tbody") + }) { + last_tbody + .upcast::() + .AppendChild(new_row.upcast::()) + .expect("InsertRow failed to append first row."); + } + } + } +}