Skip to content

Commit

Permalink
single_line_if tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjael committed Jan 24, 2024
1 parent 50c1c83 commit 2b9164e
Show file tree
Hide file tree
Showing 16 changed files with 941 additions and 37 deletions.
16 changes: 0 additions & 16 deletions tests/source/single-line-if-else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,6 @@ fn main() {
do_something()
}

let a = if x { 1 } else { 3 };

// if may be formatted on a single line if it is "short"
// and only contain a single expression
if true { return }

if true {
return
}

if true { return; }

if a { let y = 1; return y }

for i in 0..2 { if g == true { continue } }

let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa } else { bbbbbbbbbb };

let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaa } else {
Expand Down
109 changes: 109 additions & 0 deletions tests/source/single-line-simple-if.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// rustfmt-single_line_simple_if: true
// rustfmt-unstable_features: true

fn main() {
// if statements may be formatted on a single line if they are "short"
// and only contain a single expression of 'return', 'continue' or 'break'
if true { continue }

if true {
continue
}

// Default max width is 50
if width == 50_characters_or_shorter { continue }
if width == 51_characters_long_and_above { return }

if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }

// More than 1 stmt means a new line, it is no longer 'simple'
if a { let y = 1; return y; }

// Adds a semicolon to 'return/continue/break' when put on a new line
// (unless config has trailing_semicolon = false)
if a { let y = 1; return y }

// Will not work on fn or method calls (may change)
if true { do_something() }

// Will not work on an expression with trailing semicolon pre-format
if true { return; }

// Will not single line if there is an else block, even with single expressions
if true { return } else { break }

// Will not be single line if returns/breaks with a value
for i in 0..2{
if true { break }
if true { break 2 }
if true { return }
if true { return 3 }
}

// Will not be single line if comment is in the block
if true {
// nope
return
}
if true { /* nope 2 */ return }

// Only works on if blocks, not other control flow
for i in 0..2 { if i == 1 { continue } }

for i in 0..2 {
loop { if i == 1 { continue } }
}

// New line formatted here as 'loop' != 'return/continue/break'
if i == 1 { loop { return } }

// Works on labelled break/continue
'gamer: loop { if true{ break 'gamer } }

'gamer: loop { if true{ break 'gamer; } }

let result = 'block: {
if foo() { break 'block 1 }
if bar() { break 'block 2; }
3
};

#[allow(unused)]
// Comments after attributes dont mess it up
if true { return }
#[cfg(target_os = "linux")]
// Comments after attributes dont mess it up
if name == super_duper_ultra_really_name { return }
#[cfg(target_os = "linux")]
/* Multiple lines dont mess this up */
/* Multiple lines dont mess this up */
if name == super_duper_ultra_really_name { return }

// Works as intended with nested ifs and indents
if true {
if true { continue }
if true { if true { continue } }
} else if false {
if true { if true { if width == 50_characters_or_shorter { continue } if width == 51_characters_long_and_above { return } } }
} else {
if true { return; }
}

// Works with complex conditions
if matches!(x, Ok(Some(value))) { continue }
if matches!(x, Ok(Some(value))) { kick_ball() }
if matches!(x, Ok(Some(value))) && value.some_method_call(input) { break }
if matches!(x, Ok(Some(value))) && value.some_method_call(input) { run_fast() }
if matches!(x, Ok(Some(value))) && value.some_method_call(input) && single_line_if_is_allowed_at_all_ever { return }
if matches!(x, Ok(Some(value))) && value.some_method_call(input) && single_line_if_is_allowed_at_all_ever { play_catch() }

// Nested complex conditions
if true {
if matches!(x, Ok(Some(value))) { continue }
if true { if matches!(x, Ok(Some(value))) && value.some_method_call(input) { break } }
} else if false {
if true { if true { if matches!(x, Ok(Some(value))) { continue } } }
} else {
if true { if true { if matches!(x, Ok(Some(value))) && value.some_method_call(input) && single_line_if_is_allowed_at_all_ever { return } } }
}
}
43 changes: 43 additions & 0 deletions tests/source/single_line_if/false_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// rustfmt-single_line_if_else_max_width: 0

fn main() {
let a = if 1 > 2 {
unreachable!()
} else {
10
};

let a = if x { 1 } else if y { 2 } else { 3 };

if true { continue }

if true {
continue
}

if width == 50_characters_or_shorter { continue }
if width == 51_characters_long_and_above { return }

if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }

if true { return } else { break }

let x = if true { return } else { break };

let b = if cond() {
5
} else {
// Brief comment.
10
};

let c = if cond() {
statement();

5
} else {
10
};

if cond() { statement(); } else { other_statement(); }
}
43 changes: 43 additions & 0 deletions tests/source/single_line_if/false_100.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// rustfmt-single_line_if_else_max_width: 100

fn main() {
let a = if 1 > 2 {
unreachable!()
} else {
10
};

let a = if x { 1 } else if y { 2 } else { 3 };

if true { continue }

if true {
continue
}

if width == 50_characters_or_shorter { continue }
if width == 51_characters_long_and_above { return }

if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }

if true { return } else { break }

let x = if true { return } else { break };

let b = if cond() {
5
} else {
// Brief comment.
10
};

let c = if cond() {
statement();

5
} else {
10
};

if cond() { statement(); } else { other_statement(); }
}
43 changes: 43 additions & 0 deletions tests/source/single_line_if/false_50.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// rustfmt-single_line_if_else_max_width: 50

fn main() {
let a = if 1 > 2 {
unreachable!()
} else {
10
};

let a = if x { 1 } else if y { 2 } else { 3 };

if true { continue }

if true {
continue
}

if width == 50_characters_or_shorter { continue }
if width == 51_characters_long_and_above { return }

if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }

if true { return } else { break }

let x = if true { return } else { break };

let b = if cond() {
5
} else {
// Brief comment.
10
};

let c = if cond() {
statement();

5
} else {
10
};

if cond() { statement(); } else { other_statement(); }
}
45 changes: 45 additions & 0 deletions tests/source/single_line_if/true_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// rustfmt-single_line_simple_if: true
// rustfmt-unstable_features: true
// rustfmt-single_line_if_else_max_width: 0

fn main() {
let a = if 1 > 2 {
unreachable!()
} else {
10
};

let a = if x { 1 } else if y { 2 } else { 3 };

if true { continue }

if true {
continue
}

if width == 50_characters_or_shorter { continue }
if width == 51_characters_long_and_above { return }

if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }

if true { return } else { break }

let x = if true { return } else { break };

let b = if cond() {
5
} else {
// Brief comment.
10
};

let c = if cond() {
statement();

5
} else {
10
};

if cond() { statement(); } else { other_statement(); }
}
45 changes: 45 additions & 0 deletions tests/source/single_line_if/true_100.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// rustfmt-single_line_simple_if: true
// rustfmt-unstable_features: true
// rustfmt-single_line_if_else_max_width: 100

fn main() {
let a = if 1 > 2 {
unreachable!()
} else {
10
};

let a = if x { 1 } else if y { 2 } else { 3 };

if true { continue }

if true {
continue
}

if width == 50_characters_or_shorter { continue }
if width == 51_characters_long_and_above { return }

if name == super_duper_really_really_mega_ultra_giga_long_name_with_a_cherry_on_top { return }

if true { return } else { break }

let x = if true { return } else { break };

let b = if cond() {
5
} else {
// Brief comment.
10
};

let c = if cond() {
statement();

5
} else {
10
};

if cond() { statement(); } else { other_statement(); }
}
Loading

0 comments on commit 2b9164e

Please sign in to comment.