Skip to content

Commit c4314df

Browse files
authored
Merge pull request #2412 from topecongiro/issue-2399
Do not reorder items with '#[macro_use]'
2 parents c45c203 + 3bb0a2a commit c4314df

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

src/visitor.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use std::cmp;
1212

1313
use syntax::{ast, visit};
14-
use syntax::attr::HasAttrs;
14+
use syntax::attr::{self, HasAttrs};
1515
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
1616
use syntax::parse::ParseSess;
1717

@@ -49,11 +49,15 @@ fn is_mod_decl(item: &ast::Item) -> bool {
4949
}
5050
}
5151

52+
fn contains_macro_use_attr(attrs: &[ast::Attribute], span: Span) -> bool {
53+
attr::contains_name(&filter_inline_attrs(attrs, span), "macro_use")
54+
}
55+
5256
/// Returns true for `mod foo;` without any inline attributes.
5357
/// We cannot reorder modules with attributes because doing so can break the code.
5458
/// e.g. `#[macro_use]`.
5559
fn is_mod_decl_without_attr(item: &ast::Item) -> bool {
56-
is_mod_decl(item) && filter_inline_attrs(&item.attrs, item.span()).is_empty()
60+
is_mod_decl(item) && !contains_macro_use_attr(&item.attrs, item.span())
5761
}
5862

5963
fn is_use_item(item: &ast::Item) -> bool {
@@ -63,13 +67,21 @@ fn is_use_item(item: &ast::Item) -> bool {
6367
}
6468
}
6569

70+
fn is_use_item_without_attr(item: &ast::Item) -> bool {
71+
is_use_item(item) && !contains_macro_use_attr(&item.attrs, item.span())
72+
}
73+
6674
fn is_extern_crate(item: &ast::Item) -> bool {
6775
match item.node {
6876
ast::ItemKind::ExternCrate(..) => true,
6977
_ => false,
7078
}
7179
}
7280

81+
fn is_extern_crate_without_attr(item: &ast::Item) -> bool {
82+
is_extern_crate(item) && !contains_macro_use_attr(&item.attrs, item.span())
83+
}
84+
7385
/// Creates a string slice corresponding to the specified span.
7486
pub struct SnippetProvider<'a> {
7587
/// A pointer to the content of the file we are formatting.
@@ -676,11 +688,15 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
676688
// subsequent items that have the same item kind to be reordered within
677689
// `format_imports`. Otherwise, just format the next item for output.
678690
{
679-
try_reorder_items_with!(reorder_imports, reorder_imports_in_group, is_use_item);
691+
try_reorder_items_with!(
692+
reorder_imports,
693+
reorder_imports_in_group,
694+
is_use_item_without_attr
695+
);
680696
try_reorder_items_with!(
681697
reorder_extern_crates,
682698
reorder_extern_crates_in_group,
683-
is_extern_crate
699+
is_extern_crate_without_attr
684700
);
685701
try_reorder_items_with!(reorder_modules, reorder_modules, is_mod_decl_without_attr);
686702
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-reorder_extern_crates: false
2+
3+
extern crate foo;
4+
extern crate bar;
5+
extern crate foobar;
6+
7+
#[macro_use]
8+
extern crate nom;
9+
extern crate regex;
10+
#[macro_use]
11+
extern crate log;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-reorder_extern_crates: true
2+
3+
extern crate foo;
4+
extern crate bar;
5+
extern crate foobar;
6+
7+
#[macro_use]
8+
extern crate nom;
9+
extern crate regex;
10+
#[macro_use]
11+
extern crate log;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-reorder_extern_crates: false
2+
3+
extern crate foo;
4+
extern crate bar;
5+
extern crate foobar;
6+
7+
#[macro_use]
8+
extern crate nom;
9+
extern crate regex;
10+
#[macro_use]
11+
extern crate log;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-reorder_extern_crates: true
2+
3+
extern crate bar;
4+
extern crate foo;
5+
extern crate foobar;
6+
7+
#[macro_use]
8+
extern crate nom;
9+
extern crate regex;
10+
#[macro_use]
11+
extern crate log;

0 commit comments

Comments
 (0)