Skip to content

Commit 2bf67b6

Browse files
rchaser53topecongiro
authored andcommitted
fix TrailingWhitespace when using line breaks in macros arguments (rust-lang#3768)
1 parent ad5d9fb commit 2bf67b6

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

src/formatting.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
188188
format_lines(
189189
&mut visitor.buffer,
190190
&path,
191-
&visitor.skipped_range,
191+
&visitor.skipped_range.borrow(),
192192
&self.config,
193193
&self.report,
194194
);
@@ -203,7 +203,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
203203
self.report.add_macro_format_failure();
204204
}
205205
self.report
206-
.add_non_formatted_ranges(visitor.skipped_range.clone());
206+
.add_non_formatted_ranges(visitor.skipped_range.borrow().clone());
207207

208208
self.handler.handle_formatted_file(
209209
self.parse_session.source_map(),

src/macros.rs

+5
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ fn return_macro_parse_failure_fallback(
181181
return trim_left_preserve_layout(context.snippet(span), indent, &context.config);
182182
}
183183

184+
context.skipped_range.borrow_mut().push((
185+
context.source_map.lookup_line(span.lo()).unwrap().line,
186+
context.source_map.lookup_line(span.hi()).unwrap().line,
187+
));
188+
184189
// Return the snippet unmodified if the macro is not block-like
185190
Some(context.snippet(span).to_owned())
186191
}

src/rewrite.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// A generic trait to abstract the rewriting of an element (of the AST).
22

33
use std::cell::RefCell;
4+
use std::rc::Rc;
45

56
use syntax::parse::ParseSess;
67
use syntax::ptr;
@@ -41,6 +42,7 @@ pub(crate) struct RewriteContext<'a> {
4142
pub(crate) macro_rewrite_failure: RefCell<bool>,
4243
pub(crate) report: FormatReport,
4344
pub(crate) skip_context: SkipContext,
45+
pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
4446
}
4547

4648
impl<'a> RewriteContext<'a> {

src/visitor.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::cell::RefCell;
2+
use std::rc::Rc;
23

34
use syntax::parse::ParseSess;
45
use syntax::source_map::{self, BytePos, Pos, SourceMap, Span};
@@ -65,7 +66,7 @@ pub(crate) struct FmtVisitor<'a> {
6566
pub(crate) line_number: usize,
6667
/// List of 1-based line ranges which were annotated with skip
6768
/// Both bounds are inclusifs.
68-
pub(crate) skipped_range: Vec<(usize, usize)>,
69+
pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
6970
pub(crate) macro_rewrite_failure: bool,
7071
pub(crate) report: FormatReport,
7172
pub(crate) skip_context: SkipContext,
@@ -652,7 +653,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
652653
let lo = std::cmp::min(attrs_end + 1, first_line);
653654
self.push_rewrite_inner(item_span, None);
654655
let hi = self.line_number + 1;
655-
self.skipped_range.push((lo, hi));
656+
self.skipped_range.borrow_mut().push((lo, hi));
656657
}
657658

658659
pub(crate) fn from_context(ctx: &'a RewriteContext<'_>) -> FmtVisitor<'a> {
@@ -684,7 +685,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
684685
is_if_else_block: false,
685686
snippet_provider,
686687
line_number: 0,
687-
skipped_range: vec![],
688+
skipped_range: Rc::new(RefCell::new(vec![])),
688689
macro_rewrite_failure: false,
689690
report,
690691
skip_context: Default::default(),
@@ -877,6 +878,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
877878
macro_rewrite_failure: RefCell::new(false),
878879
report: self.report.clone(),
879880
skip_context: self.skip_context.clone(),
881+
skipped_range: self.skipped_range.clone(),
880882
}
881883
}
882884
}

tests/target/issue-2916.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a_macro!(name<Param1, Param2>,
2+
);

0 commit comments

Comments
 (0)