Skip to content

Commit fa242a5

Browse files
committed
Fix #1858 - "Don't erase a use with attributes attached"
This prevents code like #[cfg(unix)] pub use self::unix::{}; from becoming #[cfg(unix)] which would cause the attribute to be attached to the next item.
1 parent fc95e28 commit fa242a5

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/imports.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,28 @@ impl<'a> FmtVisitor<'a> {
250250
self.last_pos = pos_after_last_use_item;
251251
}
252252

253-
pub fn format_import(&mut self, vis: &ast::Visibility, vp: &ast::ViewPath, span: Span) {
253+
pub fn format_import(
254+
&mut self,
255+
vis: &ast::Visibility,
256+
vp: &ast::ViewPath,
257+
span: Span,
258+
attrs: &[ast::Attribute],
259+
) {
254260
let vis = utils::format_visibility(vis);
255261
// 4 = `use `, 1 = `;`
256262
let rw = Shape::indented(self.block_indent, self.config)
257263
.offset_left(vis.len() + 4)
258264
.and_then(|shape| shape.sub_width(1))
259-
.and_then(|shape| vp.rewrite(&self.get_context(), shape));
265+
.and_then(|shape| match vp.node {
266+
// If we have an empty path with attributes attached, we want to skip erasing it
267+
ast::ViewPath_::ViewPathList(ref path, ref path_list)
268+
if path_list.is_empty() && !attrs.is_empty() =>
269+
{
270+
rewrite_path(&self.get_context(), PathContext::Import, None, path, shape)
271+
.map(|path_str| format!("{}::{{}}", path_str))
272+
}
273+
_ => vp.rewrite(&self.get_context(), shape),
274+
});
260275
match rw {
261276
Some(ref s) if s.is_empty() => {
262277
// Format up to last newline

src/visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'a> FmtVisitor<'a> {
293293

294294
match item.node {
295295
ast::ItemKind::Use(ref vp) => {
296-
self.format_import(&item.vis, vp, item.span);
296+
self.format_import(&item.vis, vp, item.span, &item.attrs);
297297
}
298298
ast::ItemKind::Impl(..) => {
299299
self.format_missing_with_indent(source!(self, item.span).lo);

tests/source/imports.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,7 @@ use ::*;
7272
// spaces used to cause glob imports to disappear (#1356)
7373
use super:: * ;
7474
use foo::issue_1356:: * ;
75+
76+
// We shouldn't remove imports which have attributes attached (#1858)
77+
#[cfg(unix)]
78+
use self::unix::{};

tests/target/imports.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ use ::*;
6767
// spaces used to cause glob imports to disappear (#1356)
6868
use super::*;
6969
use foo::issue_1356::*;
70+
71+
// We shouldn't remove imports which have attributes attached (#1858)
72+
#[cfg(unix)]
73+
use self::unix::{};

0 commit comments

Comments
 (0)