Skip to content

Commit e0459eb

Browse files
scampitopecongiro
authored andcommitted
inline the attribute with its item even with the macro_use attribute or when reorder_imports is disabled (#3598)
1 parent 2244f32 commit e0459eb

11 files changed

+137
-42
lines changed

src/items.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use syntax::source_map::{self, BytePos, Span};
99
use syntax::visit;
1010
use syntax::{ast, ptr, symbol};
1111

12+
use crate::attr::filter_inline_attrs;
1213
use crate::comment::{
1314
combine_strs_with_missing_comments, contains_comment, is_last_comment_block,
1415
recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment,
@@ -2977,29 +2978,71 @@ impl Rewrite for ast::ForeignItem {
29772978
}
29782979
}
29792980

2981+
/// Rewrite the attributes of an item.
2982+
fn rewrite_attrs(
2983+
context: &RewriteContext<'_>,
2984+
item: &ast::Item,
2985+
item_str: &str,
2986+
shape: Shape,
2987+
) -> Option<String> {
2988+
let attrs = filter_inline_attrs(&item.attrs, item.span());
2989+
let attrs_str = attrs.rewrite(context, shape)?;
2990+
2991+
let missed_span = if attrs.is_empty() {
2992+
mk_sp(item.span.lo(), item.span.lo())
2993+
} else {
2994+
mk_sp(attrs[attrs.len() - 1].span.hi(), item.span.lo())
2995+
};
2996+
2997+
let allow_extend = if attrs.len() == 1 {
2998+
let line_len = attrs_str.len() + 1 + item_str.len();
2999+
!attrs.first().unwrap().is_sugared_doc
3000+
&& context.config.inline_attribute_width() >= line_len
3001+
} else {
3002+
false
3003+
};
3004+
3005+
combine_strs_with_missing_comments(
3006+
context,
3007+
&attrs_str,
3008+
&item_str,
3009+
missed_span,
3010+
shape,
3011+
allow_extend,
3012+
)
3013+
}
3014+
29803015
/// Rewrite an inline mod.
2981-
pub(crate) fn rewrite_mod(context: &RewriteContext<'_>, item: &ast::Item) -> String {
3016+
/// The given shape is used to format the mod's attributes.
3017+
pub(crate) fn rewrite_mod(
3018+
context: &RewriteContext<'_>,
3019+
item: &ast::Item,
3020+
attrs_shape: Shape,
3021+
) -> Option<String> {
29823022
let mut result = String::with_capacity(32);
29833023
result.push_str(&*format_visibility(context, &item.vis));
29843024
result.push_str("mod ");
29853025
result.push_str(rewrite_ident(context, item.ident));
29863026
result.push(';');
2987-
result
3027+
rewrite_attrs(context, item, &result, attrs_shape)
29883028
}
29893029

2990-
/// Rewrite `extern crate foo;` WITHOUT attributes.
3030+
/// Rewrite `extern crate foo;`.
3031+
/// The given shape is used to format the extern crate's attributes.
29913032
pub(crate) fn rewrite_extern_crate(
29923033
context: &RewriteContext<'_>,
29933034
item: &ast::Item,
3035+
attrs_shape: Shape,
29943036
) -> Option<String> {
29953037
assert!(is_extern_crate(item));
29963038
let new_str = context.snippet(item.span);
2997-
Some(if contains_comment(new_str) {
3039+
let item_str = if contains_comment(new_str) {
29983040
new_str.to_owned()
29993041
} else {
30003042
let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
30013043
String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
3002-
})
3044+
};
3045+
rewrite_attrs(context, item, &item_str, attrs_shape)
30033046
}
30043047

30053048
/// Returns `true` for `mod foo;`, false for `mod foo { .. }`.

src/reorder.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ use std::cmp::{Ord, Ordering};
1111
use syntax::{ast, attr, source_map::Span, symbol::sym};
1212

1313
use crate::attr::filter_inline_attrs;
14-
use crate::comment::combine_strs_with_missing_comments;
1514
use crate::config::Config;
1615
use crate::imports::{merge_use_trees, UseTree};
1716
use crate::items::{is_mod_decl, rewrite_extern_crate, rewrite_mod};
1817
use crate::lists::{itemize_list, write_list, ListFormatting, ListItem};
19-
use crate::rewrite::{Rewrite, RewriteContext};
18+
use crate::rewrite::RewriteContext;
2019
use crate::shape::Shape;
2120
use crate::source_map::LineRangeUtils;
2221
use crate::spanned::Spanned;
@@ -70,37 +69,11 @@ fn rewrite_reorderable_item(
7069
item: &ast::Item,
7170
shape: Shape,
7271
) -> Option<String> {
73-
let attrs = filter_inline_attrs(&item.attrs, item.span());
74-
let attrs_str = attrs.rewrite(context, shape)?;
75-
76-
let missed_span = if attrs.is_empty() {
77-
mk_sp(item.span.lo(), item.span.lo())
78-
} else {
79-
mk_sp(attrs.last().unwrap().span.hi(), item.span.lo())
80-
};
81-
82-
let item_str = match item.node {
83-
ast::ItemKind::ExternCrate(..) => rewrite_extern_crate(context, item)?,
84-
ast::ItemKind::Mod(..) => rewrite_mod(context, item),
85-
_ => return None,
86-
};
87-
88-
let allow_extend = if attrs.len() == 1 {
89-
let line_len = attrs_str.len() + 1 + item_str.len();
90-
!attrs.first().unwrap().is_sugared_doc
91-
&& context.config.inline_attribute_width() >= line_len
92-
} else {
93-
false
94-
};
95-
96-
combine_strs_with_missing_comments(
97-
context,
98-
&attrs_str,
99-
&item_str,
100-
missed_span,
101-
shape,
102-
allow_extend,
103-
)
72+
match item.node {
73+
ast::ItemKind::ExternCrate(..) => rewrite_extern_crate(context, item, shape),
74+
ast::ItemKind::Mod(..) => rewrite_mod(context, item, shape),
75+
_ => None,
76+
}
10477
}
10578

10679
/// Rewrite a list of items with reordering. Every item in `items` must have

src/visitor.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
315315
.append(&mut get_skip_macro_names(&attrs));
316316

317317
let should_visit_node_again = match item.node {
318-
// For use items, skip rewriting attributes. Just check for a skip attribute.
319-
ast::ItemKind::Use(..) => {
318+
// For use/extern crate items, skip rewriting attributes but check for a skip attribute.
319+
ast::ItemKind::Use(..) | ast::ItemKind::ExternCrate(_) => {
320320
if contains_skip(attrs) {
321321
self.push_skipped_with_span(attrs.as_slice(), item.span(), item.span());
322322
false
@@ -381,8 +381,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
381381
self.push_rewrite(item.span, rw);
382382
}
383383
ast::ItemKind::ExternCrate(_) => {
384-
let rw = rewrite_extern_crate(&self.get_context(), item);
385-
self.push_rewrite(item.span, rw);
384+
let rw = rewrite_extern_crate(&self.get_context(), item, self.shape());
385+
let span = if attrs.is_empty() {
386+
item.span
387+
} else {
388+
mk_sp(attrs[0].span.lo(), item.span.hi())
389+
};
390+
self.push_rewrite(span, rw);
386391
}
387392
ast::ItemKind::Struct(..) | ast::ItemKind::Union(..) => {
388393
self.visit_struct(&StructParts::from_item(item));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-inline_attribute_width: 100
2+
3+
#[macro_use]
4+
extern crate static_assertions;
5+
6+
#[cfg(unix)]
7+
extern crate static_assertions;
8+
9+
// a comment before the attribute
10+
#[macro_use]
11+
// some comment after
12+
extern crate static_assertions;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-inline_attribute_width: 100
2+
// rustfmt-reorder_imports: false
3+
4+
#[cfg(unix)]
5+
extern crate crateb;
6+
#[cfg(unix)]
7+
extern crate cratea;
8+
9+
#[cfg(unix)]
10+
use crateb;
11+
#[cfg(unix)]
12+
use cratea;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-inline_attribute_width: 100
2+
// rustfmt-reorder_imports: true
3+
4+
#[cfg(unix)]
5+
extern crate crateb;
6+
#[cfg(unix)]
7+
extern crate cratea;
8+
9+
#[cfg(unix)]
10+
use crateb;
11+
#[cfg(unix)]
12+
use cratea;

tests/source/issue-3585/use.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-inline_attribute_width: 100
2+
3+
#[macro_use]
4+
use static_assertions;
5+
6+
#[cfg(unix)]
7+
use static_assertions;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-inline_attribute_width: 100
2+
3+
#[macro_use] extern crate static_assertions;
4+
5+
#[cfg(unix)] extern crate static_assertions;
6+
7+
// a comment before the attribute
8+
#[macro_use]
9+
// some comment after
10+
extern crate static_assertions;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-inline_attribute_width: 100
2+
// rustfmt-reorder_imports: false
3+
4+
#[cfg(unix)] extern crate crateb;
5+
#[cfg(unix)] extern crate cratea;
6+
7+
#[cfg(unix)] use crateb;
8+
#[cfg(unix)] use cratea;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-inline_attribute_width: 100
2+
// rustfmt-reorder_imports: true
3+
4+
#[cfg(unix)] extern crate cratea;
5+
#[cfg(unix)] extern crate crateb;
6+
7+
#[cfg(unix)] use cratea;
8+
#[cfg(unix)] use crateb;

tests/target/issue-3585/use.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-inline_attribute_width: 100
2+
3+
#[macro_use] use static_assertions;
4+
5+
#[cfg(unix)] use static_assertions;

0 commit comments

Comments
 (0)