mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-07 05:15:02 +00:00
Merge pull request #2412 from topecongiro/issue-2399
Do not reorder items with '#[macro_use]'
This commit is contained in:
commit
c4314df1ab
@ -11,7 +11,7 @@
|
|||||||
use std::cmp;
|
use std::cmp;
|
||||||
|
|
||||||
use syntax::{ast, visit};
|
use syntax::{ast, visit};
|
||||||
use syntax::attr::HasAttrs;
|
use syntax::attr::{self, HasAttrs};
|
||||||
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
|
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
|
||||||
use syntax::parse::ParseSess;
|
use syntax::parse::ParseSess;
|
||||||
|
|
||||||
@ -49,11 +49,15 @@ fn is_mod_decl(item: &ast::Item) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn contains_macro_use_attr(attrs: &[ast::Attribute], span: Span) -> bool {
|
||||||
|
attr::contains_name(&filter_inline_attrs(attrs, span), "macro_use")
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns true for `mod foo;` without any inline attributes.
|
/// Returns true for `mod foo;` without any inline attributes.
|
||||||
/// We cannot reorder modules with attributes because doing so can break the code.
|
/// We cannot reorder modules with attributes because doing so can break the code.
|
||||||
/// e.g. `#[macro_use]`.
|
/// e.g. `#[macro_use]`.
|
||||||
fn is_mod_decl_without_attr(item: &ast::Item) -> bool {
|
fn is_mod_decl_without_attr(item: &ast::Item) -> bool {
|
||||||
is_mod_decl(item) && filter_inline_attrs(&item.attrs, item.span()).is_empty()
|
is_mod_decl(item) && !contains_macro_use_attr(&item.attrs, item.span())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_use_item(item: &ast::Item) -> bool {
|
fn is_use_item(item: &ast::Item) -> bool {
|
||||||
@ -63,6 +67,10 @@ fn is_use_item(item: &ast::Item) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_use_item_without_attr(item: &ast::Item) -> bool {
|
||||||
|
is_use_item(item) && !contains_macro_use_attr(&item.attrs, item.span())
|
||||||
|
}
|
||||||
|
|
||||||
fn is_extern_crate(item: &ast::Item) -> bool {
|
fn is_extern_crate(item: &ast::Item) -> bool {
|
||||||
match item.node {
|
match item.node {
|
||||||
ast::ItemKind::ExternCrate(..) => true,
|
ast::ItemKind::ExternCrate(..) => true,
|
||||||
@ -70,6 +78,10 @@ fn is_extern_crate(item: &ast::Item) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_extern_crate_without_attr(item: &ast::Item) -> bool {
|
||||||
|
is_extern_crate(item) && !contains_macro_use_attr(&item.attrs, item.span())
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a string slice corresponding to the specified span.
|
/// Creates a string slice corresponding to the specified span.
|
||||||
pub struct SnippetProvider<'a> {
|
pub struct SnippetProvider<'a> {
|
||||||
/// A pointer to the content of the file we are formatting.
|
/// A pointer to the content of the file we are formatting.
|
||||||
@ -676,11 +688,15 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
// subsequent items that have the same item kind to be reordered within
|
// subsequent items that have the same item kind to be reordered within
|
||||||
// `format_imports`. Otherwise, just format the next item for output.
|
// `format_imports`. Otherwise, just format the next item for output.
|
||||||
{
|
{
|
||||||
try_reorder_items_with!(reorder_imports, reorder_imports_in_group, is_use_item);
|
try_reorder_items_with!(
|
||||||
|
reorder_imports,
|
||||||
|
reorder_imports_in_group,
|
||||||
|
is_use_item_without_attr
|
||||||
|
);
|
||||||
try_reorder_items_with!(
|
try_reorder_items_with!(
|
||||||
reorder_extern_crates,
|
reorder_extern_crates,
|
||||||
reorder_extern_crates_in_group,
|
reorder_extern_crates_in_group,
|
||||||
is_extern_crate
|
is_extern_crate_without_attr
|
||||||
);
|
);
|
||||||
try_reorder_items_with!(reorder_modules, reorder_modules, is_mod_decl_without_attr);
|
try_reorder_items_with!(reorder_modules, reorder_modules, is_mod_decl_without_attr);
|
||||||
}
|
}
|
||||||
|
11
tests/source/configs/reorder_extern_crates/false.rs
Normal file
11
tests/source/configs/reorder_extern_crates/false.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// rustfmt-reorder_extern_crates: false
|
||||||
|
|
||||||
|
extern crate foo;
|
||||||
|
extern crate bar;
|
||||||
|
extern crate foobar;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate nom;
|
||||||
|
extern crate regex;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
11
tests/source/configs/reorder_extern_crates/true.rs
Normal file
11
tests/source/configs/reorder_extern_crates/true.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// rustfmt-reorder_extern_crates: true
|
||||||
|
|
||||||
|
extern crate foo;
|
||||||
|
extern crate bar;
|
||||||
|
extern crate foobar;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate nom;
|
||||||
|
extern crate regex;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
11
tests/target/configs/reorder_extern_crates/false.rs
Normal file
11
tests/target/configs/reorder_extern_crates/false.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// rustfmt-reorder_extern_crates: false
|
||||||
|
|
||||||
|
extern crate foo;
|
||||||
|
extern crate bar;
|
||||||
|
extern crate foobar;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate nom;
|
||||||
|
extern crate regex;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
11
tests/target/configs/reorder_extern_crates/true.rs
Normal file
11
tests/target/configs/reorder_extern_crates/true.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// rustfmt-reorder_extern_crates: true
|
||||||
|
|
||||||
|
extern crate bar;
|
||||||
|
extern crate foo;
|
||||||
|
extern crate foobar;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate nom;
|
||||||
|
extern crate regex;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
Loading…
Reference in New Issue
Block a user