From 46e5f14ae2300967d57ab8d032f446c07e0ed415 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 4 Jun 2024 17:02:47 +0100 Subject: [PATCH 1/3] fix: respect trailing semicolon setting for last statements --- src/stmt.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stmt.rs b/src/stmt.rs index e3fe4ebca11..6b4728d4437 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -100,7 +100,7 @@ impl<'a> Rewrite for Stmt<'a> { shape, self.as_ast_node(), expr_type, - self.is_last_expr(), + self.is_last, ) } } @@ -110,14 +110,14 @@ fn format_stmt( shape: Shape, stmt: &ast::Stmt, expr_type: ExprType, - is_last_expr: bool, + is_last: bool, ) -> Option { skip_out_of_file_lines_range!(context, stmt.span()); let result = match stmt.kind { ast::StmtKind::Local(ref local) => local.rewrite(context, shape), ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => { - let suffix = if semicolon_for_stmt(context, stmt, is_last_expr) { + let suffix = if semicolon_for_stmt(context, stmt, is_last) { ";" } else { "" From 2f5dfc6d78cce2b374d4806c57f7807496e1a290 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 4 Jun 2024 17:15:56 +0100 Subject: [PATCH 2/3] fmt --- src/stmt.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/stmt.rs b/src/stmt.rs index 6b4728d4437..e032fffe52e 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -95,13 +95,7 @@ impl<'a> Rewrite for Stmt<'a> { } else { ExprType::Statement }; - format_stmt( - context, - shape, - self.as_ast_node(), - expr_type, - self.is_last, - ) + format_stmt(context, shape, self.as_ast_node(), expr_type, self.is_last) } } From 1287a4b31b232402636eaeccd56b619758a919e1 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 4 Jun 2024 17:29:06 +0100 Subject: [PATCH 3/3] add a test case --- tests/source/trailing-semicolon.rs | 15 +++++++++++++++ tests/target/trailing-semicolon.rs | 15 +++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/source/trailing-semicolon.rs create mode 100644 tests/target/trailing-semicolon.rs diff --git a/tests/source/trailing-semicolon.rs b/tests/source/trailing-semicolon.rs new file mode 100644 index 00000000000..812face95d6 --- /dev/null +++ b/tests/source/trailing-semicolon.rs @@ -0,0 +1,15 @@ +// rustfmt-trailing_semicolon: false + +fn main() { + println!("{}", greet()); +} + +fn greet() -> String { + return "Hello, b!".to_string(); +} + +fn foo() {} +fn main() { + return; + foo() +} diff --git a/tests/target/trailing-semicolon.rs b/tests/target/trailing-semicolon.rs new file mode 100644 index 00000000000..a853f2cedbb --- /dev/null +++ b/tests/target/trailing-semicolon.rs @@ -0,0 +1,15 @@ +// rustfmt-trailing_semicolon: false + +fn main() { + println!("{}", greet()); +} + +fn greet() -> String { + return "Hello, b!".to_string() +} + +fn foo() {} +fn main() { + return; + foo() +}