Skip to content

Commit 518ae6a

Browse files
authored
Merge branch 'master' into issue-6470-dont-delete-white-space-on-triple-colon
2 parents 2ce99b0 + fd0ea74 commit 518ae6a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1618
-402
lines changed

Configurations.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,42 @@ fn foo() {
18621862
}
18631863
```
18641864

1865+
## `match_arm_indent`
1866+
1867+
Controls whether match arms are indented. If disabled, match arms will be formatted at the same indentation level as the outer `match` statement. Meaning that match blocks will only be indented once, not twice.
1868+
1869+
- **Default value**: `true`
1870+
- **Possible values**: `true`, `false`
1871+
- **Stable**: No (tracking issue: [#6533](https://github.com/rust-lang/rustfmt/issues/6533))
1872+
1873+
#### `true` (default):
1874+
1875+
```rust
1876+
fn main() {
1877+
match value {
1878+
Enum::A => {
1879+
let mut work = first();
1880+
work += second();
1881+
}
1882+
Enum::B => short_work(),
1883+
}
1884+
}
1885+
```
1886+
1887+
#### `false`:
1888+
1889+
```rust
1890+
fn main() {
1891+
match value {
1892+
Enum::A => {
1893+
let mut work = first();
1894+
work += second();
1895+
}
1896+
Enum::B => short_work(),
1897+
}
1898+
}
1899+
```
1900+
18651901
## `match_block_trailing_comma`
18661902

18671903
Put a trailing comma after a block based match arm (non-block arms are not affected)

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ See [GitHub page](https://rust-lang.github.io/rustfmt/) for details.
172172

173173
The `edition` option determines the Rust language edition used for parsing the code. This is important for syntax compatibility but does not directly control formatting behavior (see [Style Editions](#style-editions)).
174174

175-
When running `cargo fmt`, the `edition` is automatically read from the `Cargo.toml` file. However, when running `rustfmt` directly the `edition` defaults to 2015 if not explicitly configured. For consistent parsing between rustfmt and `cargo fmt` you should configure the `edition`.
176-
For example in your `rustfmt.toml` file:
175+
When running `cargo fmt`, the `edition` is automatically read from the `Cargo.toml` file. However, when running `rustfmt` directly, the `edition` defaults to 2015. For consistent parsing between rustfmt and `cargo fmt`, you should configure the `edition` in your `rustfmt.toml` file:
177176

178177
```toml
179178
edition = "2018"

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2025-01-02"
2+
channel = "nightly-2025-04-02"
33
components = ["llvm-tools", "rustc-dev"]

src/bin/main.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ fn make_opts() -> Options {
167167
"Set options from command line. These settings take priority over .rustfmt.toml",
168168
"[key1=val1,key2=val2...]",
169169
);
170+
opts.optopt(
171+
"",
172+
"style-edition",
173+
"The edition of the Style Guide.",
174+
"[2015|2018|2021|2024]",
175+
);
170176

171177
if is_nightly {
172178
opts.optflag(
@@ -818,7 +824,6 @@ mod test {
818824
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
819825
let config = get_config(None, Some(options));
820826
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
821-
assert_eq!(config.overflow_delimited_expr(), true);
822827
}
823828

824829
#[nightly_only_test]
@@ -828,7 +833,6 @@ mod test {
828833
let config_file = Some(Path::new("tests/config/style-edition/just-version"));
829834
let config = get_config(config_file, Some(options));
830835
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
831-
assert_eq!(config.overflow_delimited_expr(), true);
832836
}
833837

834838
#[nightly_only_test]
@@ -873,7 +877,6 @@ mod test {
873877
]);
874878
let config = get_config(None, Some(options));
875879
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
876-
assert_eq!(config.overflow_delimited_expr(), true);
877880
}
878881

879882
#[nightly_only_test]
@@ -939,7 +942,6 @@ mod test {
939942
options.style_edition = Some(StyleEdition::Edition2024);
940943
let config = get_config(None, Some(options));
941944
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
942-
assert_eq!(config.overflow_delimited_expr(), true);
943945
}
944946

945947
#[nightly_only_test]
@@ -949,6 +951,8 @@ mod test {
949951
let config_file = Some(Path::new("tests/config/style-edition/overrides"));
950952
let config = get_config(config_file, Some(options));
951953
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
954+
// FIXME: this test doesn't really exercise anything, since
955+
// `overflow_delimited_expr` is disabled by default in edition 2024.
952956
assert_eq!(config.overflow_delimited_expr(), false);
953957
}
954958

@@ -958,9 +962,10 @@ mod test {
958962
let mut options = GetOptsOptions::default();
959963
let config_file = Some(Path::new("tests/config/style-edition/just-style-edition"));
960964
options.inline_config =
961-
HashMap::from([("overflow_delimited_expr".to_owned(), "false".to_owned())]);
965+
HashMap::from([("overflow_delimited_expr".to_owned(), "true".to_owned())]);
962966
let config = get_config(config_file, Some(options));
963-
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
964-
assert_eq!(config.overflow_delimited_expr(), false);
967+
// FIXME: this test doesn't really exercise anything, since
968+
// `overflow_delimited_expr` is disabled by default in edition 2024.
969+
assert_eq!(config.overflow_delimited_expr(), true);
965970
}
966971
}

src/chains.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ enum ChainItemKind {
195195
StructField(symbol::Ident),
196196
TupleField(symbol::Ident, bool),
197197
Await,
198+
Use,
199+
Yield,
198200
Comment(String, CommentPosition),
199201
}
200202

@@ -206,6 +208,8 @@ impl ChainItemKind {
206208
| ChainItemKind::StructField(..)
207209
| ChainItemKind::TupleField(..)
208210
| ChainItemKind::Await
211+
| ChainItemKind::Use
212+
| ChainItemKind::Yield
209213
| ChainItemKind::Comment(..) => false,
210214
}
211215
}
@@ -260,6 +264,14 @@ impl ChainItemKind {
260264
let span = mk_sp(nested.span.hi(), expr.span.hi());
261265
(ChainItemKind::Await, span)
262266
}
267+
ast::ExprKind::Use(ref nested, _) => {
268+
let span = mk_sp(nested.span.hi(), expr.span.hi());
269+
(ChainItemKind::Use, span)
270+
}
271+
ast::ExprKind::Yield(ast::YieldKind::Postfix(ref nested)) => {
272+
let span = mk_sp(nested.span.hi(), expr.span.hi());
273+
(ChainItemKind::Yield, span)
274+
}
263275
_ => {
264276
return (
265277
ChainItemKind::Parent {
@@ -307,6 +319,8 @@ impl Rewrite for ChainItem {
307319
rewrite_ident(context, ident)
308320
),
309321
ChainItemKind::Await => ".await".to_owned(),
322+
ChainItemKind::Use => ".use".to_owned(),
323+
ChainItemKind::Yield => ".yield".to_owned(),
310324
ChainItemKind::Comment(ref comment, _) => {
311325
rewrite_comment(comment, false, shape, context.config)?
312326
}
@@ -509,7 +523,9 @@ impl Chain {
509523
}),
510524
ast::ExprKind::Field(ref subexpr, _)
511525
| ast::ExprKind::Try(ref subexpr)
512-
| ast::ExprKind::Await(ref subexpr, _) => Some(SubExpr {
526+
| ast::ExprKind::Await(ref subexpr, _)
527+
| ast::ExprKind::Use(ref subexpr, _)
528+
| ast::ExprKind::Yield(ast::YieldKind::Postfix(ref subexpr)) => Some(SubExpr {
513529
expr: Self::convert_try(subexpr, context),
514530
is_method_call_receiver: false,
515531
}),

src/closures.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ fn rewrite_closure_with_block(
180180
.first()
181181
.map(|attr| attr.span.to(body.span))
182182
.unwrap_or(body.span),
183-
could_be_bare_literal: false,
184183
};
185184
let block = crate::expr::rewrite_block_with_visitor(
186185
context,
@@ -295,14 +294,14 @@ fn rewrite_closure_fn_decl(
295294
Some(ast::CoroutineKind::AsyncGen { .. }) => "async gen ",
296295
None => "",
297296
};
298-
let mover = if matches!(capture, ast::CaptureBy::Value { .. }) {
299-
"move "
300-
} else {
301-
""
297+
let capture_str = match capture {
298+
ast::CaptureBy::Value { .. } => "move ",
299+
ast::CaptureBy::Use { .. } => "use ",
300+
ast::CaptureBy::Ref => "",
302301
};
303302
// 4 = "|| {".len(), which is overconservative when the closure consists of
304303
// a single expression.
305-
let offset = binder.len() + const_.len() + immovable.len() + coro.len() + mover.len();
304+
let offset = binder.len() + const_.len() + immovable.len() + coro.len() + capture_str.len();
306305
let nested_shape = shape.shrink_left(offset, span)?.sub_width(4, span)?;
307306

308307
// 1 = |
@@ -340,7 +339,7 @@ fn rewrite_closure_fn_decl(
340339
.tactic(tactic)
341340
.preserve_newline(true);
342341
let list_str = write_list(&item_vec, &fmt)?;
343-
let mut prefix = format!("{binder}{const_}{immovable}{coro}{mover}|{list_str}|");
342+
let mut prefix = format!("{binder}{const_}{immovable}{coro}{capture_str}|{list_str}|");
344343

345344
if !ret_str.is_empty() {
346345
if prefix.contains('\n') {

src/config/file_lines.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
use itertools::Itertools;
44
use std::collections::HashMap;
55
use std::path::PathBuf;
6+
use std::sync::Arc;
67
use std::{cmp, fmt, iter, str};
78

8-
use rustc_data_structures::sync::Lrc;
99
use rustc_span::SourceFile;
1010
use serde::{Deserialize, Deserializer, Serialize, Serializer, ser};
1111
use serde_json as json;
1212
use thiserror::Error;
1313

1414
/// A range of lines in a file, inclusive of both ends.
1515
pub struct LineRange {
16-
pub(crate) file: Lrc<SourceFile>,
16+
pub(crate) file: Arc<SourceFile>,
1717
pub(crate) lo: usize,
1818
pub(crate) hi: usize,
1919
}

src/config/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ create_config! {
131131
on the same line with the pattern of arms";
132132
match_arm_leading_pipes: MatchArmLeadingPipeConfig, true,
133133
"Determines whether leading pipes are emitted on match arms";
134+
match_arm_indent: MatchArmIndent, false,
135+
"Determines whether match arms are indented";
134136
force_multiline_blocks: ForceMultilineBlocks, false,
135137
"Force multiline closure bodies and match arms to be wrapped in a block";
136138
fn_args_layout: FnArgsLayout, true,
@@ -803,6 +805,7 @@ struct_field_align_threshold = 0
803805
enum_discrim_align_threshold = 0
804806
match_arm_blocks = true
805807
match_arm_leading_pipes = "Never"
808+
match_arm_indent = true
806809
force_multiline_blocks = false
807810
fn_params_layout = "Tall"
808811
brace_style = "SameLineWhere"
@@ -889,11 +892,12 @@ binop_separator = "Front"
889892
remove_nested_parens = true
890893
combine_control_expr = true
891894
short_array_element_width_threshold = 10
892-
overflow_delimited_expr = true
895+
overflow_delimited_expr = false
893896
struct_field_align_threshold = 0
894897
enum_discrim_align_threshold = 0
895898
match_arm_blocks = true
896899
match_arm_leading_pipes = "Never"
900+
match_arm_indent = true
897901
force_multiline_blocks = false
898902
fn_params_layout = "Tall"
899903
brace_style = "SameLineWhere"

src/config/options.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,12 @@ config_option_with_style_edition_default!(
660660
RemoveNestedParens, bool, _ => true;
661661
CombineControlExpr, bool, _ => true;
662662
ShortArrayElementWidthThreshold, usize, _ => 10;
663-
OverflowDelimitedExpr, bool, Edition2024 => true, _ => false;
663+
OverflowDelimitedExpr, bool, _ => false;
664664
StructFieldAlignThreshold, usize, _ => 0;
665665
EnumDiscrimAlignThreshold, usize, _ => 0;
666666
MatchArmBlocks, bool, _ => true;
667667
MatchArmLeadingPipeConfig, MatchArmLeadingPipe, _ => MatchArmLeadingPipe::Never;
668+
MatchArmIndent, bool, _ => true;
668669
ForceMultilineBlocks, bool, _ => false;
669670
FnArgsLayout, Density, _ => Density::Tall;
670671
FnParamsLayout, Density, _ => Density::Tall;
@@ -677,7 +678,7 @@ config_option_with_style_edition_default!(
677678
BlankLinesLowerBound, usize, _ => 0;
678679
EditionConfig, Edition, _ => Edition::Edition2015;
679680
StyleEditionConfig, StyleEdition,
680-
Edition2024 => StyleEdition::Edition2024, _ => StyleEdition::Edition2015;
681+
Edition2024 => StyleEdition::Edition2024, _ => StyleEdition::Edition2015;
681682
VersionConfig, Version, Edition2024 => Version::Two, _ => Version::One;
682683
InlineAttributeWidth, usize, _ => 0;
683684
FormatGeneratedFiles, bool, _ => true;

src/expr.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ pub(crate) fn format_expr(
249249
Ok(format!("break{id_str}"))
250250
}
251251
}
252-
ast::ExprKind::Yield(ref opt_expr) => {
252+
ast::ExprKind::Yield(ast::YieldKind::Prefix(ref opt_expr)) => {
253253
if let Some(ref expr) = *opt_expr {
254254
rewrite_unary_prefix(context, "yield ", &**expr, shape)
255255
} else {
@@ -271,9 +271,11 @@ pub(crate) fn format_expr(
271271
ast::ExprKind::Try(..)
272272
| ast::ExprKind::Field(..)
273273
| ast::ExprKind::MethodCall(..)
274-
| ast::ExprKind::Await(_, _) => rewrite_chain(expr, context, shape),
274+
| ast::ExprKind::Await(_, _)
275+
| ast::ExprKind::Use(_, _)
276+
| ast::ExprKind::Yield(ast::YieldKind::Postfix(_)) => rewrite_chain(expr, context, shape),
275277
ast::ExprKind::MacCall(ref mac) => {
276-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|_| {
278+
rewrite_macro(mac, context, shape, MacroPosition::Expression).or_else(|_| {
277279
wrap_str(
278280
context.snippet(expr.span).to_owned(),
279281
context.config.max_width(),

0 commit comments

Comments
 (0)