Skip to content

Commit 2b9164e

Browse files
committed
single_line_if tests
1 parent 50c1c83 commit 2b9164e

16 files changed

+941
-37
lines changed

tests/source/single-line-if-else.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,6 @@ fn main() {
3535
do_something()
3636
}
3737

38-
let a = if x { 1 } else { 3 };
39-
40-
// if may be formatted on a single line if it is "short"
41-
// and only contain a single expression
42-
if true { return }
43-
44-
if true {
45-
return
46-
}
47-
48-
if true { return; }
49-
50-
if a { let y = 1; return y }
51-
52-
for i in 0..2 { if g == true { continue } }
53-
5438
let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa } else { bbbbbbbbbb };
5539

5640
let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaa } else {

tests/source/single-line-simple-if.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// rustfmt-single_line_simple_if: true
2+
// rustfmt-unstable_features: true
3+
4+
fn main() {
5+
// if statements may be formatted on a single line if they are "short"
6+
// and only contain a single expression of 'return', 'continue' or 'break'
7+
if true { continue }
8+
9+
if true {
10+
continue
11+
}
12+
13+
// Default max width is 50
14+
if width == 50_characters_or_shorter { continue }
15+
if width == 51_characters_long_and_above { return }
16+
17+
if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }
18+
19+
// More than 1 stmt means a new line, it is no longer 'simple'
20+
if a { let y = 1; return y; }
21+
22+
// Adds a semicolon to 'return/continue/break' when put on a new line
23+
// (unless config has trailing_semicolon = false)
24+
if a { let y = 1; return y }
25+
26+
// Will not work on fn or method calls (may change)
27+
if true { do_something() }
28+
29+
// Will not work on an expression with trailing semicolon pre-format
30+
if true { return; }
31+
32+
// Will not single line if there is an else block, even with single expressions
33+
if true { return } else { break }
34+
35+
// Will not be single line if returns/breaks with a value
36+
for i in 0..2{
37+
if true { break }
38+
if true { break 2 }
39+
if true { return }
40+
if true { return 3 }
41+
}
42+
43+
// Will not be single line if comment is in the block
44+
if true {
45+
// nope
46+
return
47+
}
48+
if true { /* nope 2 */ return }
49+
50+
// Only works on if blocks, not other control flow
51+
for i in 0..2 { if i == 1 { continue } }
52+
53+
for i in 0..2 {
54+
loop { if i == 1 { continue } }
55+
}
56+
57+
// New line formatted here as 'loop' != 'return/continue/break'
58+
if i == 1 { loop { return } }
59+
60+
// Works on labelled break/continue
61+
'gamer: loop { if true{ break 'gamer } }
62+
63+
'gamer: loop { if true{ break 'gamer; } }
64+
65+
let result = 'block: {
66+
if foo() { break 'block 1 }
67+
if bar() { break 'block 2; }
68+
3
69+
};
70+
71+
#[allow(unused)]
72+
// Comments after attributes dont mess it up
73+
if true { return }
74+
#[cfg(target_os = "linux")]
75+
// Comments after attributes dont mess it up
76+
if name == super_duper_ultra_really_name { return }
77+
#[cfg(target_os = "linux")]
78+
/* Multiple lines dont mess this up */
79+
/* Multiple lines dont mess this up */
80+
if name == super_duper_ultra_really_name { return }
81+
82+
// Works as intended with nested ifs and indents
83+
if true {
84+
if true { continue }
85+
if true { if true { continue } }
86+
} else if false {
87+
if true { if true { if width == 50_characters_or_shorter { continue } if width == 51_characters_long_and_above { return } } }
88+
} else {
89+
if true { return; }
90+
}
91+
92+
// Works with complex conditions
93+
if matches!(x, Ok(Some(value))) { continue }
94+
if matches!(x, Ok(Some(value))) { kick_ball() }
95+
if matches!(x, Ok(Some(value))) && value.some_method_call(input) { break }
96+
if matches!(x, Ok(Some(value))) && value.some_method_call(input) { run_fast() }
97+
if matches!(x, Ok(Some(value))) && value.some_method_call(input) && single_line_if_is_allowed_at_all_ever { return }
98+
if matches!(x, Ok(Some(value))) && value.some_method_call(input) && single_line_if_is_allowed_at_all_ever { play_catch() }
99+
100+
// Nested complex conditions
101+
if true {
102+
if matches!(x, Ok(Some(value))) { continue }
103+
if true { if matches!(x, Ok(Some(value))) && value.some_method_call(input) { break } }
104+
} else if false {
105+
if true { if true { if matches!(x, Ok(Some(value))) { continue } } }
106+
} else {
107+
if true { if true { if matches!(x, Ok(Some(value))) && value.some_method_call(input) && single_line_if_is_allowed_at_all_ever { return } } }
108+
}
109+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-single_line_if_else_max_width: 0
2+
3+
fn main() {
4+
let a = if 1 > 2 {
5+
unreachable!()
6+
} else {
7+
10
8+
};
9+
10+
let a = if x { 1 } else if y { 2 } else { 3 };
11+
12+
if true { continue }
13+
14+
if true {
15+
continue
16+
}
17+
18+
if width == 50_characters_or_shorter { continue }
19+
if width == 51_characters_long_and_above { return }
20+
21+
if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }
22+
23+
if true { return } else { break }
24+
25+
let x = if true { return } else { break };
26+
27+
let b = if cond() {
28+
5
29+
} else {
30+
// Brief comment.
31+
10
32+
};
33+
34+
let c = if cond() {
35+
statement();
36+
37+
5
38+
} else {
39+
10
40+
};
41+
42+
if cond() { statement(); } else { other_statement(); }
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-single_line_if_else_max_width: 100
2+
3+
fn main() {
4+
let a = if 1 > 2 {
5+
unreachable!()
6+
} else {
7+
10
8+
};
9+
10+
let a = if x { 1 } else if y { 2 } else { 3 };
11+
12+
if true { continue }
13+
14+
if true {
15+
continue
16+
}
17+
18+
if width == 50_characters_or_shorter { continue }
19+
if width == 51_characters_long_and_above { return }
20+
21+
if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }
22+
23+
if true { return } else { break }
24+
25+
let x = if true { return } else { break };
26+
27+
let b = if cond() {
28+
5
29+
} else {
30+
// Brief comment.
31+
10
32+
};
33+
34+
let c = if cond() {
35+
statement();
36+
37+
5
38+
} else {
39+
10
40+
};
41+
42+
if cond() { statement(); } else { other_statement(); }
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-single_line_if_else_max_width: 50
2+
3+
fn main() {
4+
let a = if 1 > 2 {
5+
unreachable!()
6+
} else {
7+
10
8+
};
9+
10+
let a = if x { 1 } else if y { 2 } else { 3 };
11+
12+
if true { continue }
13+
14+
if true {
15+
continue
16+
}
17+
18+
if width == 50_characters_or_shorter { continue }
19+
if width == 51_characters_long_and_above { return }
20+
21+
if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }
22+
23+
if true { return } else { break }
24+
25+
let x = if true { return } else { break };
26+
27+
let b = if cond() {
28+
5
29+
} else {
30+
// Brief comment.
31+
10
32+
};
33+
34+
let c = if cond() {
35+
statement();
36+
37+
5
38+
} else {
39+
10
40+
};
41+
42+
if cond() { statement(); } else { other_statement(); }
43+
}

tests/source/single_line_if/true_0.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-single_line_simple_if: true
2+
// rustfmt-unstable_features: true
3+
// rustfmt-single_line_if_else_max_width: 0
4+
5+
fn main() {
6+
let a = if 1 > 2 {
7+
unreachable!()
8+
} else {
9+
10
10+
};
11+
12+
let a = if x { 1 } else if y { 2 } else { 3 };
13+
14+
if true { continue }
15+
16+
if true {
17+
continue
18+
}
19+
20+
if width == 50_characters_or_shorter { continue }
21+
if width == 51_characters_long_and_above { return }
22+
23+
if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }
24+
25+
if true { return } else { break }
26+
27+
let x = if true { return } else { break };
28+
29+
let b = if cond() {
30+
5
31+
} else {
32+
// Brief comment.
33+
10
34+
};
35+
36+
let c = if cond() {
37+
statement();
38+
39+
5
40+
} else {
41+
10
42+
};
43+
44+
if cond() { statement(); } else { other_statement(); }
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-single_line_simple_if: true
2+
// rustfmt-unstable_features: true
3+
// rustfmt-single_line_if_else_max_width: 100
4+
5+
fn main() {
6+
let a = if 1 > 2 {
7+
unreachable!()
8+
} else {
9+
10
10+
};
11+
12+
let a = if x { 1 } else if y { 2 } else { 3 };
13+
14+
if true { continue }
15+
16+
if true {
17+
continue
18+
}
19+
20+
if width == 50_characters_or_shorter { continue }
21+
if width == 51_characters_long_and_above { return }
22+
23+
if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }
24+
25+
if true { return } else { break }
26+
27+
let x = if true { return } else { break };
28+
29+
let b = if cond() {
30+
5
31+
} else {
32+
// Brief comment.
33+
10
34+
};
35+
36+
let c = if cond() {
37+
statement();
38+
39+
5
40+
} else {
41+
10
42+
};
43+
44+
if cond() { statement(); } else { other_statement(); }
45+
}

0 commit comments

Comments
 (0)