mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-23 13:13:17 +00:00
implement Drop from FmtVisitor in order to simplify failures passing
This commit is contained in:
parent
6f318e3cef
commit
1befc93ba0
@ -522,9 +522,6 @@ pub fn rewrite_block_with_visitor(
|
||||
let inner_attrs = attrs.map(inner_attributes);
|
||||
let label_str = rewrite_label(label);
|
||||
visitor.visit_block(block, inner_attrs.as_ref().map(|a| &**a), has_braces);
|
||||
if visitor.macro_rewrite_failure {
|
||||
context.macro_rewrite_failure.replace(true);
|
||||
}
|
||||
Some(format!("{}{}{}", prefix, label_str, visitor.buffer))
|
||||
}
|
||||
|
||||
|
@ -175,7 +175,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
|
||||
}
|
||||
|
||||
self.handler
|
||||
.handle_formatted_file(path, visitor.buffer, &mut self.report)
|
||||
.handle_formatted_file(path, visitor.buffer.to_owned(), &mut self.report)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -747,10 +747,6 @@ pub fn format_impl(
|
||||
|
||||
visitor.format_missing(item.span.hi() - BytePos(1));
|
||||
|
||||
if visitor.macro_rewrite_failure {
|
||||
context.macro_rewrite_failure.replace(true);
|
||||
}
|
||||
|
||||
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
|
||||
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
|
||||
|
||||
@ -1110,10 +1106,6 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
|
||||
|
||||
visitor.format_missing(item.span.hi() - BytePos(1));
|
||||
|
||||
if visitor.macro_rewrite_failure {
|
||||
context.macro_rewrite_failure.replace(true);
|
||||
}
|
||||
|
||||
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
|
||||
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
|
||||
|
||||
|
@ -69,10 +69,7 @@ impl Rewrite for ast::Item {
|
||||
visitor.block_indent = shape.indent;
|
||||
visitor.last_pos = self.span().lo();
|
||||
visitor.visit_item(self);
|
||||
if visitor.macro_rewrite_failure {
|
||||
context.macro_rewrite_failure.replace(true);
|
||||
}
|
||||
Some(visitor.buffer)
|
||||
Some(visitor.buffer.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,6 +60,7 @@ impl<'a> SnippetProvider<'a> {
|
||||
}
|
||||
|
||||
pub struct FmtVisitor<'a> {
|
||||
parent_context: Option<&'a RewriteContext<'a>>,
|
||||
pub parse_session: &'a ParseSess,
|
||||
pub source_map: &'a SourceMap,
|
||||
pub buffer: String,
|
||||
@ -75,7 +76,21 @@ pub struct FmtVisitor<'a> {
|
||||
pub(crate) report: FormatReport,
|
||||
}
|
||||
|
||||
impl<'a> Drop for FmtVisitor<'a> {
|
||||
fn drop(&mut self) {
|
||||
if let Some(ctx) = self.parent_context {
|
||||
if self.macro_rewrite_failure {
|
||||
ctx.macro_rewrite_failure.replace(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
fn set_parent_context(&mut self, context: &'a RewriteContext) {
|
||||
self.parent_context = Some(context);
|
||||
}
|
||||
|
||||
pub fn shape(&self) -> Shape {
|
||||
Shape::indented(self.block_indent, self.config)
|
||||
}
|
||||
@ -579,12 +594,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
}
|
||||
|
||||
pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
|
||||
FmtVisitor::from_source_map(
|
||||
let mut visitor = FmtVisitor::from_source_map(
|
||||
ctx.parse_session,
|
||||
ctx.config,
|
||||
ctx.snippet_provider,
|
||||
ctx.report.clone(),
|
||||
)
|
||||
);
|
||||
visitor.set_parent_context(ctx);
|
||||
visitor
|
||||
}
|
||||
|
||||
pub(crate) fn from_source_map(
|
||||
@ -594,6 +611,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
report: FormatReport,
|
||||
) -> FmtVisitor<'a> {
|
||||
FmtVisitor {
|
||||
parent_context: None,
|
||||
parse_session,
|
||||
source_map: parse_session.source_map(),
|
||||
buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),
|
||||
|
Loading…
Reference in New Issue
Block a user