From 6c9341a9ae074e34da9054841235fb0edd100a8d Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Tue, 27 Oct 2020 18:52:02 -0400 Subject: [PATCH 1/4] Don't flatten a block containing a single macro call We no longer flatten a block that looks like this: ```rust match val { pat => { macro_call!() } } ``` Currently, rust ignores trailing semicolons in macro expansion in expression position (see https://github.com/rust-lang/rust/issues/33953) If this is changed, flattening a block with a macro call may break the user's code - the trailing semicolon will no longer parse if the macro call occurs immediately on the right-hand side of the match arm (e.g. `pat => macro_call!()`) --- src/matches.rs | 14 +++++++++++++- tests/target/configs/match_arm_blocks/false.rs | 4 +++- tests/target/configs/match_arm_blocks/true.rs | 4 +++- tests/target/issue-2936.rs | 4 +++- tests/target/match.rs | 12 +++++++++--- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/matches.rs b/src/matches.rs index 7b691bf6100..3f02357a400 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -284,6 +284,15 @@ fn rewrite_match_arm( ) } +fn stmt_is_expr_mac(stmt: &ast::Stmt) -> bool { + if let ast::StmtKind::Expr(expr) = &stmt.kind { + if let ast::ExprKind::MacCall(_) = &expr.kind { + return true; + } + } + false +} + fn block_can_be_flattened<'a>( context: &RewriteContext<'_>, expr: &'a ast::Expr, @@ -292,7 +301,10 @@ fn block_can_be_flattened<'a>( ast::ExprKind::Block(ref block, _) if !is_unsafe_block(block) && !context.inside_macro() - && is_simple_block(context, block, Some(&expr.attrs)) => + && is_simple_block(context, block, Some(&expr.attrs)) + // Don't flatten a block containing a macro invocation, + // since it may expand to a statement + && !stmt_is_expr_mac(&block.stmts[0]) => { Some(&*block) } diff --git a/tests/target/configs/match_arm_blocks/false.rs b/tests/target/configs/match_arm_blocks/false.rs index 4d53a96a7b7..7a9834168c7 100644 --- a/tests/target/configs/match_arm_blocks/false.rs +++ b/tests/target/configs/match_arm_blocks/false.rs @@ -5,6 +5,8 @@ fn main() { match lorem { true => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x), - false => println!("{}", sit), + false => { + println!("{}", sit) + } } } diff --git a/tests/target/configs/match_arm_blocks/true.rs b/tests/target/configs/match_arm_blocks/true.rs index d75ef03397a..eb9e34059c7 100644 --- a/tests/target/configs/match_arm_blocks/true.rs +++ b/tests/target/configs/match_arm_blocks/true.rs @@ -6,6 +6,8 @@ fn main() { true => { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x) } - false => println!("{}", sit), + false => { + println!("{}", sit) + } } } diff --git a/tests/target/issue-2936.rs b/tests/target/issue-2936.rs index 3d5207597c4..1d6eb6d6052 100644 --- a/tests/target/issue-2936.rs +++ b/tests/target/issue-2936.rs @@ -11,7 +11,9 @@ impl Something for AStruct { let err: &CStr = match err.kind { ParseErrorKind::Custom(StyleParseErrorKind::MediaQueryExpectedFeatureName( .., - )) => cstr!("PEMQExpectedFeatureName"), + )) => { + cstr!("PEMQExpectedFeatureName") + } }; } }; diff --git a/tests/target/match.rs b/tests/target/match.rs index 92abda9752e..123c4c55f6a 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -166,9 +166,15 @@ fn issue355() { a => println!("a", b), b => vec![1, 2], c => vec![3; 4], - d => println!("a", b), - e => vec![1, 2], - f => vec![3; 4], + d => { + println!("a", b) + } + e => { + vec![1, 2] + } + f => { + vec![3; 4] + } h => println!("a", b), // h comment i => vec![1, 2], // i comment j => vec![3; 4], // j comment From 7c24c1e643232ff55f217014787742020382a830 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Thu, 5 Nov 2020 19:24:51 -0600 Subject: [PATCH 2/4] fix: remove comment from cherry-pick that v1.x doesn't handle --- src/matches.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/matches.rs b/src/matches.rs index 3f02357a400..a43aed09ef5 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -302,8 +302,6 @@ fn block_can_be_flattened<'a>( if !is_unsafe_block(block) && !context.inside_macro() && is_simple_block(context, block, Some(&expr.attrs)) - // Don't flatten a block containing a macro invocation, - // since it may expand to a statement && !stmt_is_expr_mac(&block.stmts[0]) => { Some(&*block) From fd87ec934c715c6a9e32bea1ca0e2d60c58f58b9 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Thu, 5 Nov 2020 19:30:56 -0600 Subject: [PATCH 3/4] fix: remove ignored depr attribute which now errors --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2ba476466ab..c6a9654d4d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -273,7 +273,9 @@ impl FormatReport { } } -#[deprecated(note = "Use FormatReportFormatter instead")] +/// Deprecated - Use FormatReportFormatter instead +// https://github.com/rust-lang/rust/issues/78625 +// https://github.com/rust-lang/rust/issues/39935 impl fmt::Display for FormatReport { // Prints all the formatting errors. fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { From 47fc29c9955756af82f698672ebc8a574103253d Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Thu, 5 Nov 2020 19:45:56 -0600 Subject: [PATCH 4/4] meta: release v1.4.24 --- CHANGELOG.md | 11 ++++++++++- Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c07e15f4c8..fcaf454dc55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,16 @@ ## [Unreleased] -## [1.4.22] 2020-10-30 +## [1.4.24] 2020-11-05 + +### Changed + +- Block wrapped match arm bodies containing a single macro call expression are no longer flattened ([#4496](https://github.com/rust-lang/rustfmt/pull/4496)). This allows programmer discretion so that the block wrapping can be preserved in cases where needed to prevent issues in expansion, such as with trailing semicolons, and aligns with updated [Style Guide guidance](https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/expressions.md#macro-call-expressions) for such scenarios. + +### Fixed +- Remove useless `deprecated` attribute on a trait impl block in the rustfmt lib, as these now trigger errors ([rust-lang/rust/#78626](https://github.com/rust-lang/rust/pull/78626)) + +## [1.4.23] 2020-10-30 ### Changed diff --git a/Cargo.lock b/Cargo.lock index 7f3536cbe3a..d7a586aacee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1237,7 +1237,7 @@ dependencies = [ [[package]] name = "rustfmt-nightly" -version = "1.4.23" +version = "1.4.24" dependencies = [ "annotate-snippets 0.6.1", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index c22e1b3d12c..23df8f28c16 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rustfmt-nightly" -version = "1.4.23" +version = "1.4.24" authors = ["Nicholas Cameron ", "The Rustfmt developers"] description = "Tool to find and fix Rust formatting issues" repository = "https://github.com/rust-lang/rustfmt"