Skip to content

Commit bc46af9

Browse files
ytmimicalebcartwright
authored andcommitted
Retain trailing comments in module when using rustfmt::skip attribute
Resolves 5033 Trailing comments at the end of the root Module were removed because the module span did not extend until the end of the file. The root Module's span now encompasses the entire file, which ensures that no comments are lost when using ``#![rustfmt::skip]``
1 parent 8b766f3 commit bc46af9

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/modules.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::syntux::parser::{
1616
Directory, DirectoryOwnership, ModError, ModulePathSuccess, Parser, ParserError,
1717
};
1818
use crate::syntux::session::ParseSess;
19-
use crate::utils::contains_skip;
19+
use crate::utils::{contains_skip, mk_sp};
2020

2121
mod visitor;
2222

@@ -135,10 +135,12 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
135135
self.visit_mod_from_ast(&krate.items)?;
136136
}
137137

138+
let snippet_provider = self.parse_sess.snippet_provider(krate.span);
139+
138140
self.file_map.insert(
139141
root_filename,
140142
Module::new(
141-
krate.span,
143+
mk_sp(snippet_provider.start_pos(), snippet_provider.end_pos()),
142144
None,
143145
Cow::Borrowed(&krate.items),
144146
Cow::Borrowed(&krate.attrs),
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// leading comment
2+
3+
#![rustfmt::skip]
4+
fn main() {
5+
println!("main"); // commented
6+
}
7+
8+
// post comment
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![rustfmt::skip]
2+
3+
mod a {
4+
mod b {
5+
6+
}
7+
8+
// trailing comment b
9+
}
10+
11+
// trailing comment a

0 commit comments

Comments
 (0)