diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 3b9ca90350c..06c05bb9355 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -130,7 +130,7 @@ macro_rules! create_config { pub fn $i(&mut self, value: $ty) { (self.0).$i.2 = value; match stringify!($i) { - "use_small_heuristics" => self.0.set_heuristics(), + "max_width" | "use_small_heuristics" => self.0.set_heuristics(), "license_template_path" => self.0.set_license_template(), &_ => (), } @@ -292,7 +292,7 @@ macro_rules! create_config { } match key { - "use_small_heuristics" => self.set_heuristics(), + "max_width" | "use_small_heuristics" => self.set_heuristics(), "license_template_path" => self.set_license_template(), &_ => (), } diff --git a/src/config/options.rs b/src/config/options.rs index 7bd3a51a9b4..c350aac8d96 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -242,8 +242,14 @@ impl WidthHeuristics { // scale the default WidthHeuristics according to max_width pub fn scaled(max_width: usize) -> WidthHeuristics { - let mut max_width_ratio: f32 = max_width as f32 / 100.0; // 100 is the default width -> default ratio is 1 - max_width_ratio = (max_width_ratio * 10.0).round() / 10.0; // round to the closest 0.1 + const DEFAULT_MAX_WIDTH: usize = 100; + let max_width_ratio = if max_width > DEFAULT_MAX_WIDTH { + let ratio = max_width as f32 / DEFAULT_MAX_WIDTH as f32; + // round to the closest 0.1 + (ratio * 10.0).round() / 10.0 + } else { + 1.0 + }; WidthHeuristics { fn_call_width: (60.0 * max_width_ratio).round() as usize, struct_lit_width: (18.0 * max_width_ratio).round() as usize, diff --git a/tests/source/issue-2644.rs b/tests/source/issue-2644.rs new file mode 100644 index 00000000000..fa9d16f444d --- /dev/null +++ b/tests/source/issue-2644.rs @@ -0,0 +1,11 @@ +// rustfmt-max_width: 80 +fn foo(e: Enum) { + match e { + Enum::Var { + element1, + element2, + } => { + return; + } + } +} diff --git a/tests/source/width-heuristics.rs b/tests/source/width-heuristics.rs new file mode 100644 index 00000000000..a591218b4cc --- /dev/null +++ b/tests/source/width-heuristics.rs @@ -0,0 +1,28 @@ +// rustfmt-max_width: 120 + +// elems on multiple lines for max_width 100, but same line for max_width 120 +fn foo(e: Enum) { + match e { + Enum::Var { + elem1, + elem2, + elem3, + } => { + return; + } + } +} + +// elems not on same line for either max_width 100 or 120 +fn bar(e: Enum) { + match e { + Enum::Var { + elem1, + elem2, + elem3, + elem4, + } => { + return; + } + } +} diff --git a/tests/target/issue-2644.rs b/tests/target/issue-2644.rs new file mode 100644 index 00000000000..a87e4c0b4fe --- /dev/null +++ b/tests/target/issue-2644.rs @@ -0,0 +1,8 @@ +// rustfmt-max_width: 80 +fn foo(e: Enum) { + match e { + Enum::Var { element1, element2 } => { + return; + } + } +} diff --git a/tests/target/width-heuristics.rs b/tests/target/width-heuristics.rs new file mode 100644 index 00000000000..e177a2152e8 --- /dev/null +++ b/tests/target/width-heuristics.rs @@ -0,0 +1,24 @@ +// rustfmt-max_width: 120 + +// elems on multiple lines for max_width 100, but same line for max_width 120 +fn foo(e: Enum) { + match e { + Enum::Var { elem1, elem2, elem3 } => { + return; + } + } +} + +// elems not on same line for either max_width 100 or 120 +fn bar(e: Enum) { + match e { + Enum::Var { + elem1, + elem2, + elem3, + elem4, + } => { + return; + } + } +}