diff --git a/tests/source/single-line-if-else.rs b/tests/source/single-line-if-else.rs index 832a53dedb8..bcde390d116 100644 --- a/tests/source/single-line-if-else.rs +++ b/tests/source/single-line-if-else.rs @@ -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 { diff --git a/tests/source/single-line-simple-if.rs b/tests/source/single-line-simple-if.rs new file mode 100644 index 00000000000..a7ab739d242 --- /dev/null +++ b/tests/source/single-line-simple-if.rs @@ -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 } } } + } +} \ No newline at end of file diff --git a/tests/source/single_line_if/false_0.rs b/tests/source/single_line_if/false_0.rs new file mode 100644 index 00000000000..40b090a6f1c --- /dev/null +++ b/tests/source/single_line_if/false_0.rs @@ -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(); } +} \ No newline at end of file diff --git a/tests/source/single_line_if/false_100.rs b/tests/source/single_line_if/false_100.rs new file mode 100644 index 00000000000..99316c40751 --- /dev/null +++ b/tests/source/single_line_if/false_100.rs @@ -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(); } +} diff --git a/tests/source/single_line_if/false_50.rs b/tests/source/single_line_if/false_50.rs new file mode 100644 index 00000000000..7a271a40580 --- /dev/null +++ b/tests/source/single_line_if/false_50.rs @@ -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(); } +} diff --git a/tests/source/single_line_if/true_0.rs b/tests/source/single_line_if/true_0.rs new file mode 100644 index 00000000000..4a4f02e7ddd --- /dev/null +++ b/tests/source/single_line_if/true_0.rs @@ -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(); } +} diff --git a/tests/source/single_line_if/true_100.rs b/tests/source/single_line_if/true_100.rs new file mode 100644 index 00000000000..adc4ef56ff3 --- /dev/null +++ b/tests/source/single_line_if/true_100.rs @@ -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(); } +} diff --git a/tests/source/single_line_if/true_50.rs b/tests/source/single_line_if/true_50.rs new file mode 100644 index 00000000000..9f44df4e181 --- /dev/null +++ b/tests/source/single_line_if/true_50.rs @@ -0,0 +1,45 @@ +// rustfmt-single_line_simple_if: true +// rustfmt-unstable_features: true +// 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(); } +} diff --git a/tests/target/single-line-if-else.rs b/tests/target/single-line-if-else.rs index 002f576b01a..98fd793cba2 100644 --- a/tests/target/single-line-if-else.rs +++ b/tests/target/single-line-if-else.rs @@ -42,27 +42,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 { diff --git a/tests/target/single-line-simple-if.rs b/tests/target/single-line-simple-if.rs new file mode 100644 index 00000000000..e0982811e91 --- /dev/null +++ b/tests/target/single-line-simple-if.rs @@ -0,0 +1,198 @@ +// 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; + } + } + } + } +} diff --git a/tests/target/single_line_if/false_0.rs b/tests/target/single_line_if/false_0.rs new file mode 100644 index 00000000000..2d4452eecdd --- /dev/null +++ b/tests/target/single_line_if/false_0.rs @@ -0,0 +1,69 @@ +// 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(); + } +} diff --git a/tests/target/single_line_if/false_100.rs b/tests/target/single_line_if/false_100.rs new file mode 100644 index 00000000000..90d90d96212 --- /dev/null +++ b/tests/target/single_line_if/false_100.rs @@ -0,0 +1,61 @@ +// 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(); + } +} diff --git a/tests/target/single_line_if/false_50.rs b/tests/target/single_line_if/false_50.rs new file mode 100644 index 00000000000..dcc898fbcbe --- /dev/null +++ b/tests/target/single_line_if/false_50.rs @@ -0,0 +1,61 @@ +// 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(); + } +} diff --git a/tests/target/single_line_if/true_0.rs b/tests/target/single_line_if/true_0.rs new file mode 100644 index 00000000000..fb03f4415c4 --- /dev/null +++ b/tests/target/single_line_if/true_0.rs @@ -0,0 +1,65 @@ +// 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(); + } +} diff --git a/tests/target/single_line_if/true_100.rs b/tests/target/single_line_if/true_100.rs new file mode 100644 index 00000000000..a73305bfc1a --- /dev/null +++ b/tests/target/single_line_if/true_100.rs @@ -0,0 +1,57 @@ +// 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(); + } +} diff --git a/tests/target/single_line_if/true_50.rs b/tests/target/single_line_if/true_50.rs new file mode 100644 index 00000000000..2ef738846aa --- /dev/null +++ b/tests/target/single_line_if/true_50.rs @@ -0,0 +1,57 @@ +// rustfmt-single_line_simple_if: true +// rustfmt-unstable_features: true +// 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(); + } +}