Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e633f2b

Browse files
authored
Merge pull request rust-lang#3109 from scampi/issue-3038
force a newline after the `if` condition if there is a different indentation level
2 parents 750b252 + 30c06da commit e633f2b

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

src/expr.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,20 @@ impl<'a> ControlFlow<'a> {
801801
}
802802
}
803803

804+
/// Returns true if the last line of pat_str has leading whitespace and it is wider than the
805+
/// shape's indent.
806+
fn last_line_offsetted(start_column: usize, pat_str: &str) -> bool {
807+
let mut leading_whitespaces = 0;
808+
for c in pat_str.chars().rev() {
809+
match c {
810+
'\n' => break,
811+
_ if c.is_whitespace() => leading_whitespaces += 1,
812+
_ => leading_whitespaces = 0,
813+
}
814+
}
815+
leading_whitespaces > start_column
816+
}
817+
804818
impl<'a> ControlFlow<'a> {
805819
fn rewrite_pat_expr(
806820
&self,
@@ -885,7 +899,8 @@ impl<'a> ControlFlow<'a> {
885899
.saturating_sub(constr_shape.used_width() + offset + brace_overhead);
886900
let force_newline_brace = (pat_expr_string.contains('\n')
887901
|| pat_expr_string.len() > one_line_budget)
888-
&& !last_line_extendable(&pat_expr_string);
902+
&& (!last_line_extendable(&pat_expr_string)
903+
|| last_line_offsetted(shape.used_width(), &pat_expr_string));
889904

890905
// Try to format if-else on single line.
891906
if self.allow_single_line
@@ -1977,3 +1992,29 @@ pub fn is_method_call(expr: &ast::Expr) -> bool {
19771992
_ => false,
19781993
}
19791994
}
1995+
1996+
#[cfg(test)]
1997+
mod test {
1998+
use super::last_line_offsetted;
1999+
2000+
#[test]
2001+
fn test_last_line_offsetted() {
2002+
let lines = "one\n two";
2003+
assert_eq!(last_line_offsetted(2, lines), true);
2004+
assert_eq!(last_line_offsetted(4, lines), false);
2005+
assert_eq!(last_line_offsetted(6, lines), false);
2006+
2007+
let lines = "one two";
2008+
assert_eq!(last_line_offsetted(2, lines), false);
2009+
assert_eq!(last_line_offsetted(0, lines), false);
2010+
2011+
let lines = "\ntwo";
2012+
assert_eq!(last_line_offsetted(2, lines), false);
2013+
assert_eq!(last_line_offsetted(0, lines), false);
2014+
2015+
let lines = "one\n two three";
2016+
assert_eq!(last_line_offsetted(2, lines), true);
2017+
let lines = "one\n two three";
2018+
assert_eq!(last_line_offsetted(2, lines), false);
2019+
}
2020+
}

tests/source/issue-3038.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl HTMLTableElement {
2+
fn func() {
3+
if number_of_row_elements == 0 {
4+
if let Some(last_tbody) = node.rev_children()
5+
.filter_map(DomRoot::downcast::<Element>)
6+
.find(|n| n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")) {
7+
last_tbody.upcast::<Node>().AppendChild(new_row.upcast::<Node>())
8+
.expect("InsertRow failed to append first row.");
9+
}
10+
}
11+
12+
if number_of_row_elements == 0 {
13+
if let Some(last_tbody) = node
14+
.find(|n| n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")) {
15+
last_tbody.upcast::<Node>().AppendChild(new_row.upcast::<Node>())
16+
.expect("InsertRow failed to append first row.");
17+
}
18+
}
19+
}
20+
}

tests/target/issue-2985.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ fn foo() {
2727
.map(String::as_ref)
2828
.unwrap_or("")
2929
.is_empty()
30-
}) {
30+
})
31+
{
3132
do_something();
3233
}
3334
}

tests/target/issue-3038.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
impl HTMLTableElement {
2+
fn func() {
3+
if number_of_row_elements == 0 {
4+
if let Some(last_tbody) = node
5+
.rev_children()
6+
.filter_map(DomRoot::downcast::<Element>)
7+
.find(|n| {
8+
n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")
9+
})
10+
{
11+
last_tbody
12+
.upcast::<Node>()
13+
.AppendChild(new_row.upcast::<Node>())
14+
.expect("InsertRow failed to append first row.");
15+
}
16+
}
17+
18+
if number_of_row_elements == 0 {
19+
if let Some(last_tbody) = node.find(|n| {
20+
n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")
21+
}) {
22+
last_tbody
23+
.upcast::<Node>()
24+
.AppendChild(new_row.upcast::<Node>())
25+
.expect("InsertRow failed to append first row.");
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)