Skip to content

Commit c6c8159

Browse files
authored
docs: clarify the editions behavior and it's consequences on formatting (#6486)
Update `style_edition` and `edition` entries in `Configurations.md`, and encourage users to explicitly configure `style_edition` and `edition` in `rustfmt.toml` to ensure rustfmt and `cargo fmt` produce the same formatting.
1 parent 96264d2 commit c6c8159

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

Configurations.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,17 @@ Specifies which edition is used by the parser.
537537
- **Possible values**: `"2015"`, `"2018"`, `"2021"`, `"2024"`
538538
- **Stable**: Yes
539539

540-
Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if executed
541-
through the Cargo's formatting tool `cargo fmt`. Otherwise, the edition needs to be specified
542-
in your config file:
540+
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_edition](#style_edition)).
541+
542+
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`.
543+
For example in your `rustfmt.toml` file:
543544

544545
```toml
545546
edition = "2018"
546547
```
547548

549+
Alternatively, you can use the `--edition` flag when running `rustfmt` directly.
550+
548551
## `empty_item_single_line`
549552

550553
Put empty-body functions and impls on a single line
@@ -2803,6 +2806,20 @@ Controls the edition of the [Rust Style Guide] to use for formatting ([RFC 3338]
28032806
- **Possible values**: `"2015"`, `"2018"`, `"2021"`, `"2024"` (unstable variant)
28042807
- **Stable**: No
28052808

2809+
This option is inferred from the [`edition`](#edition) if not specified.
2810+
2811+
See [Rust Style Editions] for details on formatting differences between style editions.
2812+
rustfmt has a default style edition of `2015` while `cargo fmt` infers the style edition from the `edition` set in `Cargo.toml`. This can lead to inconsistencies between `rustfmt` and `cargo fmt` if the style edition is not explicitly configured.
2813+
2814+
To ensure consistent formatting, it is recommended to specify the `style_edition` in a `rustfmt.toml` configuration file. For example:
2815+
2816+
```toml
2817+
style_edition = "2024"
2818+
```
2819+
2820+
Alternatively, you can use the `--style-edition` flag when running `rustfmt` directly.
2821+
2822+
[Rust Style Editions]: https://doc.rust-lang.org/nightly/style-guide/editions.html?highlight=editions#rust-style-editions
28062823
[Rust Style Guide]: https://doc.rust-lang.org/nightly/style-guide/
28072824
[RFC 3338]: https://rust-lang.github.io/rfcs/3338-style-evolution.html
28082825

README.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,36 @@ See [GitHub page](https://rust-lang.github.io/rustfmt/) for details.
170170

171171
### Rust's Editions
172172

173-
Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if
174-
executed through the Cargo's formatting tool `cargo fmt`. Otherwise, the edition
175-
needs to be specified in `rustfmt.toml`, e.g., with `edition = "2018"`.
173+
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)).
174+
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:
177+
178+
```toml
179+
edition = "2018"
180+
```
181+
182+
### Style Editions
183+
184+
This option is inferred from the [`edition`](#rusts-editions) if not specified.
185+
186+
See [Rust Style Editions] for details on formatting differences between style editions.
187+
rustfmt has a default style edition of `2015` while `cargo fmt` infers the style edition from the `edition` set in `Cargo.toml`. This can lead to inconsistencies between `rustfmt` and `cargo fmt` if the style edition is not explicitly configured.
188+
189+
To ensure consistent formatting, it is recommended to specify the `style_edition` in a `rustfmt.toml` configuration file. For example:
190+
191+
```toml
192+
style_edition = "2024"
193+
```
194+
[Rust Style Editions]: https://doc.rust-lang.org/nightly/style-guide/editions.html?highlight=editions#rust-style-editions
195+
[Rust Style Guide]: https://doc.rust-lang.org/nightly/style-guide/
196+
[RFC 3338]: https://rust-lang.github.io/rfcs/3338-style-evolution.html
176197

177198
## Tips
178199

200+
* To ensure consistent parsing between `cargo fmt` and `rustfmt`, you should configure the [`edition`](#rusts-editions) in your `rustfmt.toml` file.
201+
* To ensure consistent formatting between `cargo fmt` and `rustfmt`, you should configure the [`style_edition`](#style-editions) in your `rustfmt.toml` file.
202+
179203
* For things you do not want rustfmt to mangle, use `#[rustfmt::skip]`
180204
* To prevent rustfmt from formatting a macro or an attribute,
181205
use `#[rustfmt::skip::macros(target_macro_name)]` or

0 commit comments

Comments
 (0)