Skip to content

Commit

Permalink
Merge pull request rust-lang#3523 from topecongiro/issue-3515
Browse files Browse the repository at this point in the history
Attempt to format attributes if only they exist
  • Loading branch information
topecongiro authored Apr 24, 2019
2 parents aeb556e + ec0d263 commit a6daccc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ matrix:
- env: CFG_RELEASE_CHANNEL=beta
- os: osx
- env: INTEGRATION=bitflags
- env: INTEGRATION=cargo
- env: INTEGRATION=chalk
- env: INTEGRATION=crater
- env: INTEGRATION=error-chain
Expand All @@ -41,6 +40,8 @@ matrix:
- env: INTEGRATION=rust-semverver
# can be moved back to include section after https://github.com/rust-lang-nursery/failure/pull/298 is merged
- env: INTEGRATION=failure
# `cargo test` doesn't finish - disabling for now.
# - env: INTEGRATION=cargo

script:
- |
Expand Down
47 changes: 24 additions & 23 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,30 +242,31 @@ impl UseTree {
format!("{}use {};", vis, s)
}
})?;
if let Some(ref attrs) = self.attrs {
let attr_str = attrs.rewrite(context, shape)?;
let lo = attrs.last().as_ref()?.span().hi();
let hi = self.span.lo();
let span = mk_sp(lo, hi);

let allow_extend = if attrs.len() == 1 {
let line_len = attr_str.len() + 1 + use_str.len();
!attrs.first().unwrap().is_sugared_doc
&& context.config.inline_attribute_width() >= line_len
} else {
false
};
match self.attrs {
Some(ref attrs) if !attrs.is_empty() => {
let attr_str = attrs.rewrite(context, shape)?;
let lo = attrs.last().as_ref()?.span().hi();
let hi = self.span.lo();
let span = mk_sp(lo, hi);

let allow_extend = if attrs.len() == 1 {
let line_len = attr_str.len() + 1 + use_str.len();
!attrs.first().unwrap().is_sugared_doc
&& context.config.inline_attribute_width() >= line_len
} else {
false
};

combine_strs_with_missing_comments(
context,
&attr_str,
&use_str,
span,
shape,
allow_extend,
)
} else {
Some(use_str)
combine_strs_with_missing_comments(
context,
&attr_str,
&use_str,
span,
shape,
allow_extend,
)
}
_ => Some(use_str),
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/source/issue-3515.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-reorder_imports: false

use std :: fmt :: { self , Display } ;
use std :: collections :: HashMap ;

fn main() {}
6 changes: 6 additions & 0 deletions tests/target/issue-3515.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-reorder_imports: false

use std::fmt::{self, Display};
use std::collections::HashMap;

fn main() {}
2 changes: 1 addition & 1 deletion tests/target/long-use-statement-issue-3154.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// rustfmt-reorder_imports: false

pub use self :: super :: super :: super :: root::mozilla::detail::StringClassFlags as nsTStringRepr_ClassFlags ;
pub use self::super::super::super::root::mozilla::detail::StringClassFlags as nsTStringRepr_ClassFlags;

0 comments on commit a6daccc

Please sign in to comment.