Skip to content

Commit

Permalink
Merge pull request rust-lang#3109 from scampi/issue-3038
Browse files Browse the repository at this point in the history
force a newline after the `if` condition if there is a different indentation level
  • Loading branch information
nrc authored Oct 18, 2018
2 parents 750b252 + 30c06da commit e633f2b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
43 changes: 42 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions tests/source/issue-3038.rs
Original file line number Diff line number Diff line change
@@ -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::<Element>)
.find(|n| n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")) {
last_tbody.upcast::<Node>().AppendChild(new_row.upcast::<Node>())
.expect("InsertRow failed to append first row.");
}
}

if number_of_row_elements == 0 {
if let Some(last_tbody) = node
.find(|n| n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")) {
last_tbody.upcast::<Node>().AppendChild(new_row.upcast::<Node>())
.expect("InsertRow failed to append first row.");
}
}
}
}
3 changes: 2 additions & 1 deletion tests/target/issue-2985.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ fn foo() {
.map(String::as_ref)
.unwrap_or("")
.is_empty()
}) {
})
{
do_something();
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/target/issue-3038.rs
Original file line number Diff line number Diff line change
@@ -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::<Element>)
.find(|n| {
n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")
})
{
last_tbody
.upcast::<Node>()
.AppendChild(new_row.upcast::<Node>())
.expect("InsertRow failed to append first row.");
}
}

if number_of_row_elements == 0 {
if let Some(last_tbody) = node.find(|n| {
n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")
}) {
last_tbody
.upcast::<Node>()
.AppendChild(new_row.upcast::<Node>())
.expect("InsertRow failed to append first row.");
}
}
}
}

0 comments on commit e633f2b

Please sign in to comment.