Skip to content

Commit

Permalink
Catch attributes before comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Jan 26, 2017
1 parent b5d4421 commit 2261947
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ impl<'a> FmtVisitor<'a> {
let big_diff = (span.lo - big_span_lo).to_usize();
let snippet = self.snippet(span);

debug!("write_snippet `{}`", snippet);

self.write_snippet_inner(big_snippet, big_diff, &snippet, process_last_snippet);
}

Expand Down Expand Up @@ -114,6 +116,8 @@ impl<'a> FmtVisitor<'a> {
let snippet = &*replaced;

for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
debug!("{:?}: {:?}", kind, subslice);

if let CodeCharKind::Comment = kind {
let last_char = big_snippet[..(offset + big_diff)]
.chars()
Expand Down Expand Up @@ -199,6 +203,13 @@ impl<'a> FmtVisitor<'a> {
last_wspace = None;
}
}

let remaining = snippet[line_start..subslice.len() + offset].trim();
if !remaining.is_empty() {
self.buffer.push_str(remaining);
line_start = subslice.len() + offset;
rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
}
}

process_last_snippet(self, &snippet[line_start..], snippet);
Expand Down
7 changes: 7 additions & 0 deletions tests/source/comment4.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)] // bar

//! Doc comment
fn test() {
// comment
Expand Down Expand Up @@ -44,3 +46,8 @@ fn debug_function() {
println!("hello");
}
// */

#[link_section=".vectors"]
#[no_mangle] // Test this attribute is preserved.
#[cfg_attr(rustfmt, rustfmt_skip)]
pub static ISSUE_1284: [i32; 16] = [];
7 changes: 7 additions & 0 deletions tests/target/comment4.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)] // bar

//! Doc comment
fn test() {
// comment
Expand Down Expand Up @@ -43,3 +45,8 @@ fn debug_function() {
println!("hello");
}
// */

#[link_section=".vectors"]
#[no_mangle] // Test this attribute is preserved.
#[cfg_attr(rustfmt, rustfmt_skip)]
pub static ISSUE_1284: [i32; 16] = [];
2 changes: 1 addition & 1 deletion tests/target/fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn ______________________baz(a: i32)
*mut ::std::option::Option<extern "C" fn(arg1: i32,
_____________________a: i32,
arg3: i32)
-> ()> {
-> ()>{
}

pub fn check_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
Expand Down

0 comments on commit 2261947

Please sign in to comment.