Skip to content

Comments on extern crates are inconsistently indented #3128

New issue

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

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

Already on GitHub? # to your account

Closed
shepmaster opened this issue Oct 22, 2018 · 1 comment
Closed

Comments on extern crates are inconsistently indented #3128

shepmaster opened this issue Oct 22, 2018 · 1 comment

Comments

@shepmaster
Copy link
Member

0.99.6-nightly (2018-10-18 750b252)

Before

extern crate serde; // 1.0.78
extern crate serde_derive; // 1.0.78
extern crate serde_json; // 1.0.27

After

extern crate serde;        // 1.0.78
extern crate serde_derive; // 1.0.78
extern crate serde_json; // 1.0.27

Expected

Either of

extern crate serde;        // 1.0.78
extern crate serde_derive; // 1.0.78
extern crate serde_json;   // 1.0.27
extern crate serde; // 1.0.78
extern crate serde_derive; // 1.0.78
extern crate serde_json; // 1.0.27
@scampi
Copy link
Contributor

scampi commented Oct 25, 2018

I think the core problem of this issue is similar to #3127 : the span for the list of extern statements does not include the last comment, i.e., // 1.0.27 here. Therefore, that comment is not aligned with the first two. The last comment is added thanks to code in src/missed_spans.rs.

Some context:

The walk_mod_items passes the list of mod items to be reordered alphabetically:
https://github.com/rust-lang-nursery/rustfmt/blob/284583fade72a8824153b36cceabe42118b57138/src/visitor.rs#L696-L698

Since the span of that list does not include the last comment, the ListItems misses it as a post_comment of the serde_json item.

https://github.com/rust-lang-nursery/rustfmt/blob/284583fade72a8824153b36cceabe42118b57138/src/lists.rs#L702

As a consequence, when the list is written down in write_list, it is as if the comment // 1.0.27 was never there to begin with.

https://github.com/rust-lang-nursery/rustfmt/blob/284583fade72a8824153b36cceabe42118b57138/src/lists.rs#L265

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants