Skip to content

Commit b1b2dc8

Browse files
authored
Merge pull request #1859 from porglezomp-misc/master
Fix #1858 - "Don't erase a use with attributes attached"
2 parents fc95e28 + 94df4f8 commit b1b2dc8

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

src/imports.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,8 @@ fn rewrite_view_path_prefix(
157157
}
158158

159159
impl Rewrite for ast::ViewPath {
160-
// Returns an empty string when the ViewPath is empty (like foo::bar::{})
161160
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
162161
match self.node {
163-
ast::ViewPath_::ViewPathList(_, ref path_list) if path_list.is_empty() => {
164-
Some(String::new())
165-
}
166162
ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
167163
rewrite_use_list(shape, path, path_list, self.span, context)
168164
}
@@ -250,13 +246,27 @@ impl<'a> FmtVisitor<'a> {
250246
self.last_pos = pos_after_last_use_item;
251247
}
252248

253-
pub fn format_import(&mut self, vis: &ast::Visibility, vp: &ast::ViewPath, span: Span) {
249+
pub fn format_import(
250+
&mut self,
251+
vis: &ast::Visibility,
252+
vp: &ast::ViewPath,
253+
span: Span,
254+
attrs: &[ast::Attribute],
255+
) {
254256
let vis = utils::format_visibility(vis);
255257
// 4 = `use `, 1 = `;`
256258
let rw = Shape::indented(self.block_indent, self.config)
257259
.offset_left(vis.len() + 4)
258260
.and_then(|shape| shape.sub_width(1))
259-
.and_then(|shape| vp.rewrite(&self.get_context(), shape));
261+
.and_then(|shape| match vp.node {
262+
// If we have an empty path list with no attributes, we erase it
263+
ast::ViewPath_::ViewPathList(_, ref path_list)
264+
if path_list.is_empty() && attrs.is_empty() =>
265+
{
266+
Some("".into())
267+
}
268+
_ => vp.rewrite(&self.get_context(), shape),
269+
});
260270
match rw {
261271
Some(ref s) if s.is_empty() => {
262272
// Format up to last newline
@@ -385,7 +395,7 @@ impl<'a> Ord for ImportItem<'a> {
385395
}
386396

387397
// Pretty prints a multi-item import.
388-
// Assumes that path_list.len() > 0.
398+
// If the path list is empty, it leaves the braces empty.
389399
fn rewrite_use_list(
390400
shape: Shape,
391401
path: &ast::Path,
@@ -403,7 +413,10 @@ fn rewrite_use_list(
403413
));
404414

405415
match path_list.len() {
406-
0 => unreachable!(),
416+
0 => {
417+
return rewrite_path(context, PathContext::Import, None, path, shape)
418+
.map(|path_str| format!("{}::{{}}", path_str));
419+
}
407420
1 => return Some(rewrite_single_use_list(path_str, &path_list[0])),
408421
_ => (),
409422
}

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)