Skip to content

Commit 6a75fee

Browse files
authored
Merge pull request #3409 from rchaser53/issue-3343
add config inline_attribute_width
2 parents 29a597c + be7b3ba commit 6a75fee

File tree

6 files changed

+150
-2
lines changed

6 files changed

+150
-2
lines changed

Configurations.md

+21
Original file line numberDiff line numberDiff line change
@@ -2408,6 +2408,27 @@ pub enum Foo {}
24082408
pub enum Foo {}
24092409
```
24102410

2411+
## `inline_attribute_width`
2412+
2413+
Write an item and its attribute on the same line if their combined width is below a threshold
2414+
2415+
- **Default value**: 0
2416+
- **Possible values**: any positive integer
2417+
- **Stable**: No (tracking issue: #3343)
2418+
2419+
### Example
2420+
2421+
#### `0` (default):
2422+
```rust
2423+
#[cfg(feature = "alloc")]
2424+
use core::slice;
2425+
```
2426+
2427+
#### `50`:
2428+
```rust
2429+
#[cfg(feature = "alloc")] use core::slice;
2430+
```
2431+
24112432
## `emit_mode`
24122433

24132434
Internal option

src/config/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ create_config! {
105105
"Minimum number of blank lines which must be put between items";
106106
edition: Edition, Edition::Edition2015, true, "The edition of the parser (RFC 2052)";
107107
version: Version, Version::One, false, "Version of formatting rules";
108+
inline_attribute_width: usize, 0, false,
109+
"Write an item and its attribute on the same line \
110+
if their combined width is below a threshold";
108111

109112
// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
110113
merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one";

src/imports.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,23 @@ impl UseTree {
249249
let lo = attrs.last().as_ref()?.span().hi();
250250
let hi = self.span.lo();
251251
let span = mk_sp(lo, hi);
252-
combine_strs_with_missing_comments(context, &attr_str, &use_str, span, shape, false)
252+
253+
let allow_extend = if attrs.len() == 1 {
254+
let line_len = attr_str.len() + 1 + use_str.len();
255+
!attrs.first().unwrap().is_sugared_doc
256+
&& context.config.inline_attribute_width() >= line_len
257+
} else {
258+
false
259+
};
260+
261+
combine_strs_with_missing_comments(
262+
context,
263+
&attr_str,
264+
&use_str,
265+
span,
266+
shape,
267+
allow_extend,
268+
)
253269
} else {
254270
Some(use_str)
255271
}

src/reorder.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,22 @@ fn rewrite_reorderable_item(
8383
_ => return None,
8484
};
8585

86-
combine_strs_with_missing_comments(context, &attrs_str, &item_str, missed_span, shape, false)
86+
let allow_extend = if attrs.len() == 1 {
87+
let line_len = attrs_str.len() + 1 + item_str.len();
88+
!attrs.first().unwrap().is_sugared_doc
89+
&& context.config.inline_attribute_width() >= line_len
90+
} else {
91+
false
92+
};
93+
94+
combine_strs_with_missing_comments(
95+
context,
96+
&attrs_str,
97+
&item_str,
98+
missed_span,
99+
shape,
100+
allow_extend,
101+
)
87102
}
88103

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

tests/source/issue-3343.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// rustfmt-inline_attribute_width: 50
2+
3+
#[cfg(feature = "alloc")]
4+
use core::slice;
5+
6+
#[cfg(feature = "alloc")]
7+
use total_len_is::_50__;
8+
9+
#[cfg(feature = "alloc")]
10+
use total_len_is::_51___;
11+
12+
#[cfg(feature = "alloc")]
13+
extern crate len_is_50_;
14+
15+
#[cfg(feature = "alloc")]
16+
extern crate len_is_51__;
17+
18+
/// this is a comment to test is_sugared_doc property
19+
use core::convert;
20+
21+
#[fooooo]
22+
#[barrrrr]
23+
use total_len_is_::_51______;
24+
25+
#[cfg(not(all(
26+
feature = "std",
27+
any(
28+
target_os = "linux",
29+
target_os = "android",
30+
target_os = "netbsd",
31+
target_os = "dragonfly",
32+
target_os = "haiku",
33+
target_os = "emscripten",
34+
target_os = "solaris",
35+
target_os = "cloudabi",
36+
target_os = "macos",
37+
target_os = "ios",
38+
target_os = "freebsd",
39+
target_os = "openbsd",
40+
target_os = "bitrig",
41+
target_os = "redox",
42+
target_os = "fuchsia",
43+
windows,
44+
all(target_arch = "wasm32", feature = "stdweb"),
45+
all(target_arch = "wasm32", feature = "wasm-bindgen"),
46+
)
47+
)))]
48+
use core::slice;

tests/target/issue-3343.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-inline_attribute_width: 50
2+
3+
#[cfg(feature = "alloc")] use core::slice;
4+
5+
#[cfg(feature = "alloc")] use total_len_is::_50__;
6+
7+
#[cfg(feature = "alloc")]
8+
use total_len_is::_51___;
9+
10+
#[cfg(feature = "alloc")] extern crate len_is_50_;
11+
12+
#[cfg(feature = "alloc")]
13+
extern crate len_is_51__;
14+
15+
/// this is a comment to test is_sugared_doc property
16+
use core::convert;
17+
18+
#[fooooo]
19+
#[barrrrr]
20+
use total_len_is_::_51______;
21+
22+
#[cfg(not(all(
23+
feature = "std",
24+
any(
25+
target_os = "linux",
26+
target_os = "android",
27+
target_os = "netbsd",
28+
target_os = "dragonfly",
29+
target_os = "haiku",
30+
target_os = "emscripten",
31+
target_os = "solaris",
32+
target_os = "cloudabi",
33+
target_os = "macos",
34+
target_os = "ios",
35+
target_os = "freebsd",
36+
target_os = "openbsd",
37+
target_os = "bitrig",
38+
target_os = "redox",
39+
target_os = "fuchsia",
40+
windows,
41+
all(target_arch = "wasm32", feature = "stdweb"),
42+
all(target_arch = "wasm32", feature = "wasm-bindgen"),
43+
)
44+
)))]
45+
use core::slice;

0 commit comments

Comments
 (0)