Skip to content

Commit

Permalink
Retain trailing comments in module when using rustfmt::skip attribute
Browse files Browse the repository at this point in the history
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]``
  • Loading branch information
ytmimi authored and calebcartwright committed Oct 29, 2021
1 parent 8b766f3 commit bc46af9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/modules.rs
Expand Up @@ -16,7 +16,7 @@ use crate::syntux::parser::{
Directory, DirectoryOwnership, ModError, ModulePathSuccess, Parser, ParserError,
};
use crate::syntux::session::ParseSess;
use crate::utils::contains_skip;
use crate::utils::{contains_skip, mk_sp};

mod visitor;

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

let snippet_provider = self.parse_sess.snippet_provider(krate.span);

self.file_map.insert(
root_filename,
Module::new(
krate.span,
mk_sp(snippet_provider.start_pos(), snippet_provider.end_pos()),
None,
Cow::Borrowed(&krate.items),
Cow::Borrowed(&krate.attrs),
Expand Down
8 changes: 8 additions & 0 deletions tests/target/issue-5033/minimum_example.rs
@@ -0,0 +1,8 @@
// leading comment

#![rustfmt::skip]
fn main() {
println!("main"); // commented
}

// post comment
11 changes: 11 additions & 0 deletions tests/target/issue-5033/nested_modules.rs
@@ -0,0 +1,11 @@
#![rustfmt::skip]

mod a {
mod b {

}

// trailing comment b
}

// trailing comment a

0 comments on commit bc46af9

Please sign in to comment.