11
11
use std:: cmp;
12
12
13
13
use syntax:: { ast, visit} ;
14
- use syntax:: attr:: HasAttrs ;
14
+ use syntax:: attr:: { self , HasAttrs } ;
15
15
use syntax:: codemap:: { self , BytePos , CodeMap , Pos , Span } ;
16
16
use syntax:: parse:: ParseSess ;
17
17
@@ -49,11 +49,15 @@ fn is_mod_decl(item: &ast::Item) -> bool {
49
49
}
50
50
}
51
51
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
+
52
56
/// Returns true for `mod foo;` without any inline attributes.
53
57
/// We cannot reorder modules with attributes because doing so can break the code.
54
58
/// e.g. `#[macro_use]`.
55
59
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 ( ) )
57
61
}
58
62
59
63
fn is_use_item ( item : & ast:: Item ) -> bool {
@@ -63,13 +67,21 @@ fn is_use_item(item: &ast::Item) -> bool {
63
67
}
64
68
}
65
69
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
+
66
74
fn is_extern_crate ( item : & ast:: Item ) -> bool {
67
75
match item. node {
68
76
ast:: ItemKind :: ExternCrate ( ..) => true ,
69
77
_ => false ,
70
78
}
71
79
}
72
80
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
+
73
85
/// Creates a string slice corresponding to the specified span.
74
86
pub struct SnippetProvider < ' a > {
75
87
/// A pointer to the content of the file we are formatting.
@@ -676,11 +688,15 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
676
688
// subsequent items that have the same item kind to be reordered within
677
689
// `format_imports`. Otherwise, just format the next item for output.
678
690
{
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
+ ) ;
680
696
try_reorder_items_with ! (
681
697
reorder_extern_crates,
682
698
reorder_extern_crates_in_group,
683
- is_extern_crate
699
+ is_extern_crate_without_attr
684
700
) ;
685
701
try_reorder_items_with ! ( reorder_modules, reorder_modules, is_mod_decl_without_attr) ;
686
702
}
0 commit comments