Skip to content

Commit 2261947

Browse files
committed
Catch attributes before comments
Closes #1280 Closes #1284
1 parent b5d4421 commit 2261947

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/missed_spans.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ impl<'a> FmtVisitor<'a> {
8484
let big_diff = (span.lo - big_span_lo).to_usize();
8585
let snippet = self.snippet(span);
8686

87+
debug!("write_snippet `{}`", snippet);
88+
8789
self.write_snippet_inner(big_snippet, big_diff, &snippet, process_last_snippet);
8890
}
8991

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

116118
for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
119+
debug!("{:?}: {:?}", kind, subslice);
120+
117121
if let CodeCharKind::Comment = kind {
118122
let last_char = big_snippet[..(offset + big_diff)]
119123
.chars()
@@ -199,6 +203,13 @@ impl<'a> FmtVisitor<'a> {
199203
last_wspace = None;
200204
}
201205
}
206+
207+
let remaining = snippet[line_start..subslice.len() + offset].trim();
208+
if !remaining.is_empty() {
209+
self.buffer.push_str(remaining);
210+
line_start = subslice.len() + offset;
211+
rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
212+
}
202213
}
203214

204215
process_last_snippet(self, &snippet[line_start..], snippet);

tests/source/comment4.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)] // bar
2+
13
//! Doc comment
24
fn test() {
35
// comment
@@ -44,3 +46,8 @@ fn debug_function() {
4446
println!("hello");
4547
}
4648
// */
49+
50+
#[link_section=".vectors"]
51+
#[no_mangle] // Test this attribute is preserved.
52+
#[cfg_attr(rustfmt, rustfmt_skip)]
53+
pub static ISSUE_1284: [i32; 16] = [];

tests/target/comment4.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)] // bar
2+
13
//! Doc comment
24
fn test() {
35
// comment
@@ -43,3 +45,8 @@ fn debug_function() {
4345
println!("hello");
4446
}
4547
// */
48+
49+
#[link_section=".vectors"]
50+
#[no_mangle] // Test this attribute is preserved.
51+
#[cfg_attr(rustfmt, rustfmt_skip)]
52+
pub static ISSUE_1284: [i32; 16] = [];

tests/target/fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn ______________________baz(a: i32)
8686
*mut ::std::option::Option<extern "C" fn(arg1: i32,
8787
_____________________a: i32,
8888
arg3: i32)
89-
-> ()> {
89+
-> ()>{
9090
}
9191

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

0 commit comments

Comments
 (0)