Skip to content

Commit 04705fe

Browse files
authored
Merge pull request #4455 from rust-lang/issue-4059.rs
try to write the parameter on a new line in case the attribute/parameter together are over max_width
2 parents 7e31d5d + 85daa36 commit 04705fe

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

src/formatting/comment.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::formatting::{
1212
string::{rewrite_string, StringFormat},
1313
utils::{
1414
count_newlines, first_line_width, format_code_block, last_line_width, tab_to_spaces,
15-
trim_end_unless_two_whitespaces, trim_left_preserve_layout, unicode_str_width,
15+
trim_end_unless_two_whitespaces, trim_left_preserve_layout, trimmed_last_line_width,
16+
unicode_str_width,
1617
},
1718
};
1819

@@ -177,11 +178,12 @@ pub(crate) fn combine_strs_with_missing_comments(
177178
String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128);
178179
result.push_str(prev_str);
179180
let mut allow_one_line = !prev_str.contains('\n') && !next_str.contains('\n');
180-
let first_sep = if prev_str.is_empty() || next_str.is_empty() {
181-
""
182-
} else {
183-
" "
184-
};
181+
let first_sep =
182+
if prev_str.is_empty() || next_str.is_empty() || trimmed_last_line_width(prev_str) == 0 {
183+
""
184+
} else {
185+
" "
186+
};
185187
let mut one_line_width =
186188
last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
187189

src/formatting/items.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -2142,12 +2142,13 @@ impl Rewrite for ast::Param {
21422142
has_multiple_attr_lines,
21432143
)
21442144
} else if is_named_param(self) {
2145+
let param_name = &self
2146+
.pat
2147+
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
21452148
let mut result = combine_strs_with_missing_comments(
21462149
context,
21472150
&param_attrs_result,
2148-
&self
2149-
.pat
2150-
.rewrite(context, Shape::legacy(shape.width, shape.indent))?,
2151+
param_name,
21512152
span,
21522153
shape,
21532154
!has_multiple_attr_lines,
@@ -2161,10 +2162,30 @@ impl Rewrite for ast::Param {
21612162
result.push_str(&after_comment);
21622163
let overhead = last_line_width(&result);
21632164
let max_width = shape.width.checked_sub(overhead)?;
2164-
let ty_str = self
2165+
if let Some(ty_str) = self
21652166
.ty
2166-
.rewrite(context, Shape::legacy(max_width, shape.indent))?;
2167-
result.push_str(&ty_str);
2167+
.rewrite(context, Shape::legacy(max_width, shape.indent))
2168+
{
2169+
result.push_str(&ty_str);
2170+
} else {
2171+
result = combine_strs_with_missing_comments(
2172+
context,
2173+
&(param_attrs_result + &shape.to_string_with_newline(context.config)),
2174+
param_name,
2175+
span,
2176+
shape,
2177+
!has_multiple_attr_lines,
2178+
)?;
2179+
result.push_str(&before_comment);
2180+
result.push_str(colon_spaces(context.config));
2181+
result.push_str(&after_comment);
2182+
let overhead = last_line_width(&result);
2183+
let max_width = shape.width.checked_sub(overhead)?;
2184+
let ty_str = self
2185+
.ty
2186+
.rewrite(context, Shape::legacy(max_width, shape.indent))?;
2187+
result.push_str(&ty_str);
2188+
}
21682189
}
21692190

21702191
Some(result)

tests/target/issue_4032.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
fn a1(
2-
#[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] a: u8,
2+
#[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
3+
a: u8,
34
) {
45
}
56
fn b1(
6-
#[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] bb: u8,
7+
#[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
8+
bb: u8,
79
) {
810
}
911
fn a2(

0 commit comments

Comments
 (0)