Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1befc93

Browse files
committed
implement Drop from FmtVisitor in order to simplify failures passing
1 parent 6f318e3 commit 1befc93

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

src/expr.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,6 @@ pub fn rewrite_block_with_visitor(
522522
let inner_attrs = attrs.map(inner_attributes);
523523
let label_str = rewrite_label(label);
524524
visitor.visit_block(block, inner_attrs.as_ref().map(|a| &**a), has_braces);
525-
if visitor.macro_rewrite_failure {
526-
context.macro_rewrite_failure.replace(true);
527-
}
528525
Some(format!("{}{}{}", prefix, label_str, visitor.buffer))
529526
}
530527

src/formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
175175
}
176176

177177
self.handler
178-
.handle_formatted_file(path, visitor.buffer, &mut self.report)
178+
.handle_formatted_file(path, visitor.buffer.to_owned(), &mut self.report)
179179
}
180180
}
181181

src/items.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,6 @@ pub fn format_impl(
747747

748748
visitor.format_missing(item.span.hi() - BytePos(1));
749749

750-
if visitor.macro_rewrite_failure {
751-
context.macro_rewrite_failure.replace(true);
752-
}
753-
754750
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
755751
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
756752

@@ -1110,10 +1106,6 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
11101106

11111107
visitor.format_missing(item.span.hi() - BytePos(1));
11121108

1113-
if visitor.macro_rewrite_failure {
1114-
context.macro_rewrite_failure.replace(true);
1115-
}
1116-
11171109
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
11181110
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
11191111

src/macros.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ impl Rewrite for ast::Item {
6969
visitor.block_indent = shape.indent;
7070
visitor.last_pos = self.span().lo();
7171
visitor.visit_item(self);
72-
if visitor.macro_rewrite_failure {
73-
context.macro_rewrite_failure.replace(true);
74-
}
75-
Some(visitor.buffer)
72+
Some(visitor.buffer.to_owned())
7673
}
7774
}
7875

src/visitor.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl<'a> SnippetProvider<'a> {
6060
}
6161

6262
pub struct FmtVisitor<'a> {
63+
parent_context: Option<&'a RewriteContext<'a>>,
6364
pub parse_session: &'a ParseSess,
6465
pub source_map: &'a SourceMap,
6566
pub buffer: String,
@@ -75,7 +76,21 @@ pub struct FmtVisitor<'a> {
7576
pub(crate) report: FormatReport,
7677
}
7778

79+
impl<'a> Drop for FmtVisitor<'a> {
80+
fn drop(&mut self) {
81+
if let Some(ctx) = self.parent_context {
82+
if self.macro_rewrite_failure {
83+
ctx.macro_rewrite_failure.replace(true);
84+
}
85+
}
86+
}
87+
}
88+
7889
impl<'b, 'a: 'b> FmtVisitor<'a> {
90+
fn set_parent_context(&mut self, context: &'a RewriteContext) {
91+
self.parent_context = Some(context);
92+
}
93+
7994
pub fn shape(&self) -> Shape {
8095
Shape::indented(self.block_indent, self.config)
8196
}
@@ -579,12 +594,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
579594
}
580595

581596
pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
582-
FmtVisitor::from_source_map(
597+
let mut visitor = FmtVisitor::from_source_map(
583598
ctx.parse_session,
584599
ctx.config,
585600
ctx.snippet_provider,
586601
ctx.report.clone(),
587-
)
602+
);
603+
visitor.set_parent_context(ctx);
604+
visitor
588605
}
589606

590607
pub(crate) fn from_source_map(
@@ -594,6 +611,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
594611
report: FormatReport,
595612
) -> FmtVisitor<'a> {
596613
FmtVisitor {
614+
parent_context: None,
597615
parse_session,
598616
source_map: parse_session.source_map(),
599617
buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),

0 commit comments

Comments
 (0)