Rustfmt: Formatting fixes
Summary
- Fixes to various formatting scenarios.
Details
The 2024 style edition introduces several fixes to various formatting scenarios.
Don't align unrelated trailing comments after items or at the end of blocks
Previously rustfmt would assume that a comment on a line following an item with a trailing comment should be indented to match the trailing comment. This has been changed so that those comments are not indented.
Style edition 2021:
pub const IFF_MULTICAST: c_int = 0x0000000800; // Supports multicast
// Multicast using broadcst. add.
pub const SQ_CRETAB: u16 = 0x000e; // CREATE TABLE
pub const SQ_DRPTAB: u16 = 0x000f; // DROP TABLE
pub const SQ_CREIDX: u16 = 0x0010; // CREATE INDEX
//const SQ_DRPIDX: u16 = 0x0011; // DROP INDEX
//const SQ_GRANT: u16 = 0x0012; // GRANT
//const SQ_REVOKE: u16 = 0x0013; // REVOKE
Style edition 2024:
pub const IFF_MULTICAST: c_int = 0x0000000800; // Supports multicast
// Multicast using broadcst. add.
pub const SQ_CRETAB: u16 = 0x000e; // CREATE TABLE
pub const SQ_DRPTAB: u16 = 0x000f; // DROP TABLE
pub const SQ_CREIDX: u16 = 0x0010; // CREATE INDEX
//const SQ_DRPIDX: u16 = 0x0011; // DROP INDEX
//const SQ_GRANT: u16 = 0x0012; // GRANT
//const SQ_REVOKE: u16 = 0x0013; // REVOKE
Don't indent strings in comments
Previously rustfmt would incorrectly attempt to format strings in comments.
Original:
Style edition 2021:
Style edition 2024:
No change from original.
Long strings don't prevent formatting expressions
In some situations, long strings would previously prevent the expression from being formatted.
Style edition 2021:
Style edition 2024:
Fixed indentation of generics in impl blocks
Generics in impl items had excessive indentation.
Style edition 2021:
Style edition 2024:
Use correct indentation when formatting a complex fn
In some cases, a complex fn signature could end up with an unusual indentation that is now fixed.
Style edition 2021:
Style edition 2024:
Avoid extra space in nested tuple indexing expression
Nested tuple indexing expressions would incorrectly include an extra space.
Style edition 2021:
Style edition 2024:
End return/break/continue inside a block in a match with a semicolon
A return, break, or continue inside a block in a match arm was incorrectly missing a semicolon.
Style edition 2021:
Style edition 2024:
Long array and slice patterns are now wrapped
Long array and slice patterns were not getting wrapped properly.
Style edition 2021:
Style edition 2024:
Format the last expression-statement as an expression
The last statement in a block which is an expression is now formatted as an expression.
Style edition 2021:
Style edition 2024:
Same formatting between function and macro calls
Some formatting is now the same in a macro invocation as it is in a function call.
Style edition 2021:
Style edition 2024:
Force block closures for closures with a single loop body
Closures with a single loop are now formatted as a block expression.
Style edition 2021:
Style edition 2024:
Empty lines in where clauses are now removed
Empty lines in a where clause are now removed.
Style edition 2021:
Style edition 2024:
Fixed formatting of a let-else statement with an attribute
If a let-else statement had an attribute, then it would cause the else clause to incorrectly wrap the else part separately.
Style edition 2021:
Style edition 2024:
Off-by-one error for wrapping enum variant doc comments
When using the wrap_comments feature, the comments were being wrapped at a column width off-by-one.
Original:
Style edition 2021:
Style edition 2024:
Off-by-one error for format_macro_matchers
When using the format_macro_matchers feature, the matcher was being wrapped at a column width off-by-one.
Style edition 2021:
Style edition 2024:
Fixed failure with => in comment after match =>
In certain circumstances if a comment contained a => after the => in a match expression, this would cause a failure to format correctly.
Style edition 2021:
Style edition 2024:
Multiple inner attributes in a match expression indented incorrectly
Multiple inner attributes in a match expression were being indented incorrectly.
Style edition 2021:
Style edition 2024:
Migration
The change can be applied automatically by running cargo fmt or rustfmt with the 2024 Edition. See the Style edition chapter for more information on migrating and how style editions work.