diff --git a/src/expr.rs b/src/expr.rs index a47f216..7b223b8 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -521,8 +521,8 @@ impl Printer { self.cbox(INDENT); self.hardbreak_if_nonempty(); self.inner_attrs(&expr.attrs); - for stmt in &expr.body.stmts { - self.stmt(stmt); + for stmt in expr.body.stmts.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } self.offset(-INDENT); self.end(); @@ -573,8 +573,8 @@ impl Printer { } else { self.word("{"); self.hardbreak(); - for stmt in &expr.then_branch.stmts { - self.stmt(stmt); + for stmt in expr.then_branch.stmts.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } self.offset(-INDENT); self.word("}"); @@ -662,8 +662,8 @@ impl Printer { self.cbox(INDENT); self.hardbreak_if_nonempty(); self.inner_attrs(&expr.attrs); - for stmt in &expr.body.stmts { - self.stmt(stmt); + for stmt in expr.body.stmts.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } self.offset(-INDENT); self.end(); @@ -1026,8 +1026,8 @@ impl Printer { self.cbox(INDENT); self.hardbreak_if_nonempty(); self.inner_attrs(&expr.attrs); - for stmt in &expr.body.stmts { - self.stmt(stmt); + for stmt in expr.body.stmts.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } self.offset(-INDENT); self.end(); @@ -1105,8 +1105,8 @@ impl Printer { self.cbox(INDENT); self.hardbreak_if_nonempty(); self.inner_attrs(&body.attrs); - for stmt in &body.block.stmts { - self.stmt(stmt); + for stmt in body.block.stmts.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } self.offset(-INDENT); self.end(); @@ -1164,8 +1164,8 @@ impl Printer { self.space(); } _ => { - for stmt in &block.stmts { - self.stmt(stmt); + for stmt in block.stmts.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } } } diff --git a/src/item.rs b/src/item.rs index 7f8033b..4e9d95d 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,6 +1,7 @@ use crate::algorithm::Printer; use crate::fixup::FixupContext; use crate::iter::IterDelimited; +use crate::mac; use crate::path::PathKind; use crate::INDENT; use proc_macro2::TokenStream; @@ -101,8 +102,8 @@ impl Printer { self.word("{"); self.hardbreak_if_nonempty(); self.inner_attrs(&item.attrs); - for stmt in &item.block.stmts { - self.stmt(stmt); + for stmt in item.block.stmts.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } self.offset(-INDENT); self.end(); @@ -169,7 +170,7 @@ impl Printer { fn item_macro(&mut self, item: &ItemMacro) { self.outer_attrs(&item.attrs); - let semicolon = true; + let semicolon = mac::requires_semi(&item.mac.delimiter); self.mac(&item.mac, item.ident.as_ref(), semicolon); self.hardbreak(); } @@ -837,7 +838,7 @@ impl Printer { fn foreign_item_macro(&mut self, foreign_item: &ForeignItemMacro) { self.outer_attrs(&foreign_item.attrs); - let semicolon = true; + let semicolon = mac::requires_semi(&foreign_item.mac.delimiter); self.mac(&foreign_item.mac, None, semicolon); self.hardbreak(); } @@ -982,8 +983,8 @@ impl Printer { self.word("{"); self.hardbreak_if_nonempty(); self.inner_attrs(&trait_item.attrs); - for stmt in &block.stmts { - self.stmt(stmt); + for stmt in block.stmts.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } self.offset(-INDENT); self.end(); @@ -1024,7 +1025,7 @@ impl Printer { fn trait_item_macro(&mut self, trait_item: &TraitItemMacro) { self.outer_attrs(&trait_item.attrs); - let semicolon = true; + let semicolon = mac::requires_semi(&trait_item.mac.delimiter); self.mac(&trait_item.mac, None, semicolon); self.hardbreak(); } @@ -1181,8 +1182,8 @@ impl Printer { self.word("{"); self.hardbreak_if_nonempty(); self.inner_attrs(&impl_item.attrs); - for stmt in &impl_item.block.stmts { - self.stmt(stmt); + for stmt in impl_item.block.stmts.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } self.offset(-INDENT); self.end(); @@ -1212,7 +1213,7 @@ impl Printer { fn impl_item_macro(&mut self, impl_item: &ImplItemMacro) { self.outer_attrs(&impl_item.attrs); - let semicolon = true; + let semicolon = mac::requires_semi(&impl_item.mac.delimiter); self.mac(&impl_item.mac, None, semicolon); self.hardbreak(); } @@ -1730,8 +1731,8 @@ mod verbatim { self.word("{"); self.hardbreak_if_nonempty(); self.inner_attrs(&item.attrs); - for stmt in body { - self.stmt(stmt); + for stmt in body.iter().delimited() { + self.stmt(&stmt, stmt.is_last); } self.offset(-INDENT); self.end(); diff --git a/src/mac.rs b/src/mac.rs index b556e68..230a8d4 100644 --- a/src/mac.rs +++ b/src/mac.rs @@ -41,10 +41,7 @@ impl Printer { } self.word(close); if semicolon { - match mac.delimiter { - MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => self.word(";"), - MacroDelimiter::Brace(_) => {} - } + self.word(";"); } } @@ -212,6 +209,13 @@ impl Printer { } } +pub(crate) fn requires_semi(delimiter: &MacroDelimiter) -> bool { + match delimiter { + MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => true, + MacroDelimiter::Brace(_) => false, + } +} + fn is_keyword(ident: &Ident) -> bool { match ident.to_string().as_str() { "as" | "async" | "await" | "box" | "break" | "const" | "continue" | "crate" | "dyn" diff --git a/src/stmt.rs b/src/stmt.rs index cc057c2..ce58200 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -2,11 +2,12 @@ use crate::algorithm::Printer; use crate::classify; use crate::expr; use crate::fixup::FixupContext; +use crate::mac; use crate::INDENT; use syn::{BinOp, Expr, Stmt}; impl Printer { - pub fn stmt(&mut self, stmt: &Stmt) { + pub fn stmt(&mut self, stmt: &Stmt, is_last: bool) { match stmt { Stmt::Local(local) => { self.outer_attrs(&local.attrs); @@ -69,7 +70,8 @@ impl Printer { } Stmt::Macro(stmt) => { self.outer_attrs(&stmt.attrs); - let semicolon = true; + let semicolon = stmt.semi_token.is_some() + || !is_last && mac::requires_semi(&stmt.mac.delimiter); self.mac(&stmt.mac, None, semicolon); self.hardbreak(); }