Skip to content

fix TrailingWhitespace when using line breaks in macros arguments #3768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
format_lines(
&mut visitor.buffer,
&path,
&visitor.skipped_range,
&visitor.skipped_range.borrow(),
&self.config,
&self.report,
);
Expand All @@ -203,7 +203,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
self.report.add_macro_format_failure();
}
self.report
.add_non_formatted_ranges(visitor.skipped_range.clone());
.add_non_formatted_ranges(visitor.skipped_range.borrow().clone());

self.handler.handle_formatted_file(
self.parse_session.source_map(),
Expand Down
5 changes: 5 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ fn return_macro_parse_failure_fallback(
return trim_left_preserve_layout(context.snippet(span), indent, &context.config);
}

context.skipped_range.borrow_mut().push((
context.source_map.lookup_line(span.lo()).unwrap().line,
context.source_map.lookup_line(span.hi()).unwrap().line,
));

// Return the snippet unmodified if the macro is not block-like
Some(context.snippet(span).to_owned())
}
Expand Down
2 changes: 2 additions & 0 deletions src/rewrite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// A generic trait to abstract the rewriting of an element (of the AST).

use std::cell::RefCell;
use std::rc::Rc;

use syntax::parse::ParseSess;
use syntax::ptr;
Expand Down Expand Up @@ -41,6 +42,7 @@ pub(crate) struct RewriteContext<'a> {
pub(crate) macro_rewrite_failure: RefCell<bool>,
pub(crate) report: FormatReport,
pub(crate) skip_context: SkipContext,
pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
Copy link
Contributor Author

@rchaser53 rchaser53 Sep 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an important point in this PR. I want to use skipped_range in RewriteContext. It's used to resolve #3717. I think the skipped_range should include the failed format span or skipped span.

This is also related with #3761. The case #3761 is a case the edge of the span has white space. It's easy to resolve. Just trim the white space. But I think no way to avoid TrailingWhitespace error now in a case the failed format span has newline now.

// case like #3761. just trim the white space.
let snippet = "     let a = 1;"; 

// case like this PR. no way to avoid TrailingWhitespace error now
a_macro!(name<Param1, Param2>, 
);

}

impl<'a> RewriteContext<'a> {
Expand Down
8 changes: 5 additions & 3 deletions src/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::rc::Rc;

use syntax::parse::ParseSess;
use syntax::source_map::{self, BytePos, Pos, SourceMap, Span};
Expand Down Expand Up @@ -65,7 +66,7 @@ pub(crate) struct FmtVisitor<'a> {
pub(crate) line_number: usize,
/// List of 1-based line ranges which were annotated with skip
/// Both bounds are inclusifs.
pub(crate) skipped_range: Vec<(usize, usize)>,
pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
pub(crate) macro_rewrite_failure: bool,
pub(crate) report: FormatReport,
pub(crate) skip_context: SkipContext,
Expand Down Expand Up @@ -652,7 +653,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
let lo = std::cmp::min(attrs_end + 1, first_line);
self.push_rewrite_inner(item_span, None);
let hi = self.line_number + 1;
self.skipped_range.push((lo, hi));
self.skipped_range.borrow_mut().push((lo, hi));
}

pub(crate) fn from_context(ctx: &'a RewriteContext<'_>) -> FmtVisitor<'a> {
Expand Down Expand Up @@ -684,7 +685,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
is_if_else_block: false,
snippet_provider,
line_number: 0,
skipped_range: vec![],
skipped_range: Rc::new(RefCell::new(vec![])),
macro_rewrite_failure: false,
report,
skip_context: Default::default(),
Expand Down Expand Up @@ -877,6 +878,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
macro_rewrite_failure: RefCell::new(false),
report: self.report.clone(),
skip_context: self.skip_context.clone(),
skipped_range: self.skipped_range.clone(),
}
}
}
2 changes: 2 additions & 0 deletions tests/target/issue-2916.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a_macro!(name<Param1, Param2>,
);