Skip to content

Commit 4a57e79

Browse files
authored
Merge pull request #2667 from tspiteri/width-heuristics
Do not scale WidthHeuristics when max_width less than 100
2 parents b6cd17f + 48df8f8 commit 4a57e79

File tree

6 files changed

+81
-4
lines changed

6 files changed

+81
-4
lines changed

src/config/config_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ macro_rules! create_config {
130130
pub fn $i(&mut self, value: $ty) {
131131
(self.0).$i.2 = value;
132132
match stringify!($i) {
133-
"use_small_heuristics" => self.0.set_heuristics(),
133+
"max_width" | "use_small_heuristics" => self.0.set_heuristics(),
134134
"license_template_path" => self.0.set_license_template(),
135135
&_ => (),
136136
}
@@ -292,7 +292,7 @@ macro_rules! create_config {
292292
}
293293

294294
match key {
295-
"use_small_heuristics" => self.set_heuristics(),
295+
"max_width" | "use_small_heuristics" => self.set_heuristics(),
296296
"license_template_path" => self.set_license_template(),
297297
&_ => (),
298298
}

src/config/options.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,14 @@ impl WidthHeuristics {
242242

243243
// scale the default WidthHeuristics according to max_width
244244
pub fn scaled(max_width: usize) -> WidthHeuristics {
245-
let mut max_width_ratio: f32 = max_width as f32 / 100.0; // 100 is the default width -> default ratio is 1
246-
max_width_ratio = (max_width_ratio * 10.0).round() / 10.0; // round to the closest 0.1
245+
const DEFAULT_MAX_WIDTH: usize = 100;
246+
let max_width_ratio = if max_width > DEFAULT_MAX_WIDTH {
247+
let ratio = max_width as f32 / DEFAULT_MAX_WIDTH as f32;
248+
// round to the closest 0.1
249+
(ratio * 10.0).round() / 10.0
250+
} else {
251+
1.0
252+
};
247253
WidthHeuristics {
248254
fn_call_width: (60.0 * max_width_ratio).round() as usize,
249255
struct_lit_width: (18.0 * max_width_ratio).round() as usize,

tests/source/issue-2644.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-max_width: 80
2+
fn foo(e: Enum) {
3+
match e {
4+
Enum::Var {
5+
element1,
6+
element2,
7+
} => {
8+
return;
9+
}
10+
}
11+
}

tests/source/width-heuristics.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// rustfmt-max_width: 120
2+
3+
// elems on multiple lines for max_width 100, but same line for max_width 120
4+
fn foo(e: Enum) {
5+
match e {
6+
Enum::Var {
7+
elem1,
8+
elem2,
9+
elem3,
10+
} => {
11+
return;
12+
}
13+
}
14+
}
15+
16+
// elems not on same line for either max_width 100 or 120
17+
fn bar(e: Enum) {
18+
match e {
19+
Enum::Var {
20+
elem1,
21+
elem2,
22+
elem3,
23+
elem4,
24+
} => {
25+
return;
26+
}
27+
}
28+
}

tests/target/issue-2644.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-max_width: 80
2+
fn foo(e: Enum) {
3+
match e {
4+
Enum::Var { element1, element2 } => {
5+
return;
6+
}
7+
}
8+
}

tests/target/width-heuristics.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// rustfmt-max_width: 120
2+
3+
// elems on multiple lines for max_width 100, but same line for max_width 120
4+
fn foo(e: Enum) {
5+
match e {
6+
Enum::Var { elem1, elem2, elem3 } => {
7+
return;
8+
}
9+
}
10+
}
11+
12+
// elems not on same line for either max_width 100 or 120
13+
fn bar(e: Enum) {
14+
match e {
15+
Enum::Var {
16+
elem1,
17+
elem2,
18+
elem3,
19+
elem4,
20+
} => {
21+
return;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)