Skip to content

Commit

Permalink
Merge pull request #103 from dtolnay/macrosemi
Browse files Browse the repository at this point in the history
Preserve semicolon on braced statement-position macros
  • Loading branch information
dtolnay authored Jan 12, 2025
2 parents 78b9b40 + 1290d70 commit 4e85487
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
24 changes: 12 additions & 12 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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("}");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/item.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 8 additions & 4 deletions src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ impl Printer {
}
self.word(close);
if semicolon {
match mac.delimiter {
MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => self.word(";"),
MacroDelimiter::Brace(_) => {}
}
self.word(";");
}
}

Expand Down Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 4e85487

Please sign in to comment.