Skip to content

Commit 6162f6f

Browse files
committedJul 1, 2023
Auto merge of rust-lang#113225 - calebcartwright:sync-rustfmt, r=calebcartwright
Update Rustfmt (add let-else support) Adds let-else formatting support Bit more detail in: https://github.com/rust-lang/rustfmt/blob/master/CHANGELOG.md#160-2023-07-02 Accompanying blog post: rust-lang/blog.rust-lang.org#1117 I know we're getting close to tool week, however, there's been extensive discussion and testing of the changes in this between both t-style and t-rustfmt. Our confidence level is extremely high, and even if it's only on nightly for a few days, I'd still much prefer that and being able to get this out with 1.72 vs having to push to 1.73 Closes rust-lang/rustfmt#4914 cc `@rust-lang/style` for awareness

File tree

26 files changed

+1082
-50
lines changed

26 files changed

+1082
-50
lines changed
 

‎Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4264,7 +4264,7 @@ dependencies = [
42644264

42654265
[[package]]
42664266
name = "rustfmt-nightly"
4267-
version = "1.5.3"
4267+
version = "1.6.0"
42684268
dependencies = [
42694269
"annotate-snippets",
42704270
"anyhow",

‎src/tools/rustfmt/CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
## [Unreleased]
44

5+
6+
## [1.6.0] 2023-07-02
7+
8+
### Added
9+
10+
- Support for formatting let-else statements [#5690]
11+
- New config option, `single_line_let_else_max_width`, that allows users to configure the maximum length of single line `let-else` statements. `let-else` statements that otherwise meet the requirements to be formatted on a single line will have their divergent`else` block formatted over multiple lines if they exceed this length [#5684]
12+
13+
[#5690]: (https://github.com/rust-lang/rustfmt/pulls/5690)
14+
[#5684]: https://github.com/rust-lang/rustfmt/issues/5684
15+
516
## [1.5.3] 2023-06-20
617

718
### Fixed

‎src/tools/rustfmt/Cargo.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ dependencies = [
481481

482482
[[package]]
483483
name = "proc-macro2"
484-
version = "1.0.56"
484+
version = "1.0.63"
485485
source = "registry+https://github.com/rust-lang/crates.io-index"
486-
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
486+
checksum = "7b368fba921b0dce7e60f5e04ec15e565b3303972b42bcfde1d0713b881959eb"
487487
dependencies = [
488488
"unicode-ident",
489489
]
@@ -545,7 +545,7 @@ dependencies = [
545545

546546
[[package]]
547547
name = "rustfmt-nightly"
548-
version = "1.5.3"
548+
version = "1.6.0"
549549
dependencies = [
550550
"annotate-snippets",
551551
"anyhow",

‎src/tools/rustfmt/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "rustfmt-nightly"
4-
version = "1.5.3"
4+
version = "1.6.0"
55
description = "Tool to find and fix Rust formatting issues"
66
repository = "https://github.com/rust-lang/rustfmt"
77
readme = "README.md"

‎src/tools/rustfmt/Configurations.md

+77
Original file line numberDiff line numberDiff line change
@@ -2392,6 +2392,78 @@ By default this option is set as a percentage of [`max_width`](#max_width) provi
23922392

23932393
See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)
23942394

2395+
## `single_line_let_else_max_width`
2396+
2397+
Maximum line length for single line let-else statements.
2398+
See the [let-else statement section of the Rust Style Guide](https://github.com/rust-lang/rust/blob/master/src/doc/style-guide/src/statements.md#else-blocks-let-else-statements) for more details on when a let-else statement may be written on a single line.
2399+
A value of `0` (zero) means the divergent `else` block will always be formatted over multiple lines.
2400+
Note this occurs when `use_small_heuristics` is set to `Off`.
2401+
2402+
By default this option is set as a percentage of [`max_width`](#max_width) provided by [`use_small_heuristics`](#use_small_heuristics), but a value set directly for `single_line_let_else_max_width` will take precedence.
2403+
2404+
- **Default value**: `50`
2405+
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
2406+
- **Stable**: Yes
2407+
2408+
#### `50` (default):
2409+
2410+
```rust
2411+
fn main() {
2412+
let Some(w) = opt else { return Ok(()) };
2413+
2414+
let Some(x) = opt else { return };
2415+
2416+
let Some(y) = opt else {
2417+
return;
2418+
};
2419+
2420+
let Some(z) = some_very_very_very_very_long_name else {
2421+
return;
2422+
};
2423+
}
2424+
```
2425+
2426+
#### `0`:
2427+
2428+
```rust
2429+
fn main() {
2430+
let Some(w) = opt else {
2431+
return Ok(());
2432+
};
2433+
2434+
let Some(x) = opt else {
2435+
return;
2436+
};
2437+
2438+
let Some(y) = opt else {
2439+
return;
2440+
};
2441+
2442+
let Some(z) = some_very_very_very_very_long_name else {
2443+
return;
2444+
};
2445+
}
2446+
```
2447+
2448+
#### `100`:
2449+
2450+
```rust
2451+
fn main() {
2452+
let Some(w) = opt else { return Ok(()) };
2453+
2454+
let Some(x) = opt else { return };
2455+
2456+
let Some(y) = opt else {
2457+
return;
2458+
};
2459+
2460+
let Some(z) = some_very_very_very_very_long_name else { return };
2461+
}
2462+
```
2463+
2464+
See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)
2465+
2466+
23952467
## `space_after_colon`
23962468

23972469
Leave a space after the colon.
@@ -2804,6 +2876,7 @@ The ratios are:
28042876
* [`array_width`](#array_width) - `60%`
28052877
* [`chain_width`](#chain_width) - `60%`
28062878
* [`single_line_if_else_max_width`](#single_line_if_else_max_width) - `50%`
2879+
* [`single_line_let_else_max_width`](#single_line_let_else_max_width) - `50%`
28072880

28082881
For example when `max_width` is set to `100`, the width settings are:
28092882
* `fn_call_width=60`
@@ -2813,6 +2886,7 @@ For example when `max_width` is set to `100`, the width settings are:
28132886
* `array_width=60`
28142887
* `chain_width=60`
28152888
* `single_line_if_else_max_width=50`
2889+
* `single_line_let_else_max_width=50`
28162890

28172891
and when `max_width` is set to `200`:
28182892
* `fn_call_width=120`
@@ -2822,6 +2896,7 @@ and when `max_width` is set to `200`:
28222896
* `array_width=120`
28232897
* `chain_width=120`
28242898
* `single_line_if_else_max_width=100`
2899+
* `single_line_let_else_max_width=100`
28252900

28262901
```rust
28272902
enum Lorem {
@@ -2891,6 +2966,7 @@ So if `max_width` is set to `200`, then all the width settings are also set to `
28912966
* `array_width=200`
28922967
* `chain_width=200`
28932968
* `single_line_if_else_max_width=200`
2969+
* `single_line_let_else_max_width=200`
28942970

28952971
```rust
28962972
enum Lorem {
@@ -2918,6 +2994,7 @@ See also:
29182994
* [`array_width`](#array_width)
29192995
* [`chain_width`](#chain_width)
29202996
* [`single_line_if_else_max_width`](#single_line_if_else_max_width)
2997+
* [`single_line_let_else_max_width`](#single_line_let_else_max_width)
29212998

29222999
## `use_try_shorthand`
29233000

‎src/tools/rustfmt/config_proc_macro/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ version = 3
44

55
[[package]]
66
name = "proc-macro2"
7-
version = "1.0.56"
7+
version = "1.0.63"
88
source = "registry+https://github.com/rust-lang/crates.io-index"
9-
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
9+
checksum = "7b368fba921b0dce7e60f5e04ec15e565b3303972b42bcfde1d0713b881959eb"
1010
dependencies = [
1111
"unicode-ident",
1212
]

‎src/tools/rustfmt/rust-toolchain

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2023-06-19"
2+
channel = "nightly-2023-07-01"
33
components = ["llvm-tools", "rustc-dev"]

‎src/tools/rustfmt/src/config/config_type.rs

+10
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ macro_rules! create_config {
121121
| "use_small_heuristics"
122122
| "fn_call_width"
123123
| "single_line_if_else_max_width"
124+
| "single_line_let_else_max_width"
124125
| "attr_fn_like_width"
125126
| "struct_lit_width"
126127
| "struct_variant_width"
@@ -269,6 +270,7 @@ macro_rules! create_config {
269270
| "use_small_heuristics"
270271
| "fn_call_width"
271272
| "single_line_if_else_max_width"
273+
| "single_line_let_else_max_width"
272274
| "attr_fn_like_width"
273275
| "struct_lit_width"
274276
| "struct_variant_width"
@@ -407,6 +409,14 @@ macro_rules! create_config {
407409
"single_line_if_else_max_width",
408410
);
409411
self.single_line_if_else_max_width.2 = single_line_if_else_max_width;
412+
413+
let single_line_let_else_max_width = get_width_value(
414+
self.was_set().single_line_let_else_max_width(),
415+
self.single_line_let_else_max_width.2,
416+
heuristics.single_line_let_else_max_width,
417+
"single_line_let_else_max_width",
418+
);
419+
self.single_line_let_else_max_width.2 = single_line_let_else_max_width;
410420
}
411421

412422
fn set_heuristics(&mut self) {

‎src/tools/rustfmt/src/config/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ create_config! {
5858
chain_width: usize, 60, true, "Maximum length of a chain to fit on a single line.";
5959
single_line_if_else_max_width: usize, 50, true, "Maximum line length for single line if-else \
6060
expressions. A value of zero means always break if-else expressions.";
61+
single_line_let_else_max_width: usize, 50, true, "Maximum line length for single line \
62+
let-else statements. A value of zero means always format the divergent `else` block \
63+
over multiple lines.";
6164

6265
// Comments. macros, and strings
6366
wrap_comments: bool, false, false, "Break comments to fit on the line";
@@ -473,6 +476,9 @@ mod test {
473476
chain_width: usize, 60, true, "Maximum length of a chain to fit on a single line.";
474477
single_line_if_else_max_width: usize, 50, true, "Maximum line length for single \
475478
line if-else expressions. A value of zero means always break if-else expressions.";
479+
single_line_let_else_max_width: usize, 50, false, "Maximum line length for single \
480+
line let-else statements. A value of zero means always format the divergent \
481+
`else` block over multiple lines.";
476482

477483
// Options that are used by the tests
478484
stable_option: bool, false, true, "A stable option";
@@ -619,6 +625,7 @@ struct_variant_width = 35
619625
array_width = 60
620626
chain_width = 60
621627
single_line_if_else_max_width = 50
628+
single_line_let_else_max_width = 50
622629
wrap_comments = false
623630
format_code_in_doc_comments = false
624631
doc_comment_code_block_width = 100

‎src/tools/rustfmt/src/config/options.rs

+6
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ pub struct WidthHeuristics {
236236
// Maximum line length for single line if-else expressions. A value
237237
// of zero means always break if-else expressions.
238238
pub(crate) single_line_if_else_max_width: usize,
239+
// Maximum line length for single line let-else statements. A value of zero means
240+
// always format the divergent `else` block over multiple lines.
241+
pub(crate) single_line_let_else_max_width: usize,
239242
}
240243

241244
impl fmt::Display for WidthHeuristics {
@@ -255,6 +258,7 @@ impl WidthHeuristics {
255258
array_width: usize::max_value(),
256259
chain_width: usize::max_value(),
257260
single_line_if_else_max_width: 0,
261+
single_line_let_else_max_width: 0,
258262
}
259263
}
260264

@@ -267,6 +271,7 @@ impl WidthHeuristics {
267271
array_width: max_width,
268272
chain_width: max_width,
269273
single_line_if_else_max_width: max_width,
274+
single_line_let_else_max_width: max_width,
270275
}
271276
}
272277

@@ -288,6 +293,7 @@ impl WidthHeuristics {
288293
array_width: (60.0 * max_width_ratio).round() as usize,
289294
chain_width: (60.0 * max_width_ratio).round() as usize,
290295
single_line_if_else_max_width: (50.0 * max_width_ratio).round() as usize,
296+
single_line_let_else_max_width: (50.0 * max_width_ratio).round() as usize,
291297
}
292298
}
293299
}

0 commit comments

Comments
 (0)
Please sign in to comment.