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+ }
0 commit comments