Skip to content

Commit dc163d8

Browse files
committed
fix: #6520, move comment above line if it would cross out the type in a function arg
1 parent 334670e commit dc163d8

File tree

4 files changed

+103
-8
lines changed

4 files changed

+103
-8
lines changed

src/comment.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,18 @@ pub(crate) fn rewrite_missing_comment(
10151015
}
10161016
}
10171017

1018+
pub(crate) fn comment_reaches_end_of_line(
1019+
span: Span,
1020+
context: &RewriteContext<'_>,
1021+
normalize_comments: bool,
1022+
) -> bool {
1023+
let missing_snippet = context.snippet(span);
1024+
let trimmed_snippet = missing_snippet.trim();
1025+
comment_style(trimmed_snippet, normalize_comments)
1026+
.closer()
1027+
.is_empty()
1028+
}
1029+
10181030
/// Recover the missing comments in the specified span, if available.
10191031
/// The layout of the comments will be preserved as long as it does not break the code
10201032
/// and its total width does not exceed the max width.

src/items.rs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use tracing::debug;
1111

1212
use crate::attr::filter_inline_attrs;
1313
use crate::comment::{
14-
FindUncommented, combine_strs_with_missing_comments, contains_comment, is_last_comment_block,
15-
recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment,
14+
FindUncommented, combine_strs_with_missing_comments, comment_reaches_end_of_line,
15+
contains_comment, is_last_comment_block, recover_comment_removed,
16+
recover_missing_comment_in_span, rewrite_missing_comment,
1617
};
1718
use crate::config::lists::*;
1819
use crate::config::{BraceStyle, Config, IndentStyle, StyleEdition};
@@ -2212,6 +2213,17 @@ fn is_empty_infer(ty: &ast::Ty, pat_span: Span) -> bool {
22122213
}
22132214
}
22142215

2216+
struct CommentAfterColon {
2217+
content: Option<String>,
2218+
reaches_end_of_line: bool,
2219+
}
2220+
2221+
impl CommentAfterColon {
2222+
fn content(&self) -> &str {
2223+
self.content.as_deref().unwrap_or("")
2224+
}
2225+
}
2226+
22152227
/// Recover any missing comments between the param and the type.
22162228
///
22172229
/// # Returns
@@ -2223,7 +2235,7 @@ fn get_missing_param_comments(
22232235
pat_span: Span,
22242236
ty_span: Span,
22252237
shape: Shape,
2226-
) -> (String, String) {
2238+
) -> (String, CommentAfterColon) {
22272239
let missing_comment_span = mk_sp(pat_span.hi(), ty_span.lo());
22282240

22292241
let span_before_colon = {
@@ -2246,7 +2258,25 @@ fn get_missing_param_comments(
22462258
let comment_after_colon = rewrite_missing_comment(span_after_colon, shape, context)
22472259
.ok()
22482260
.filter(|comment| !comment.is_empty())
2249-
.map_or(String::new(), |comment| format!("{} ", comment));
2261+
.map_or(
2262+
CommentAfterColon {
2263+
content: None,
2264+
reaches_end_of_line: false,
2265+
},
2266+
|comment| {
2267+
let reaches_end_of_line =
2268+
comment_reaches_end_of_line(span_after_colon, context, false);
2269+
let content = if reaches_end_of_line {
2270+
comment
2271+
} else {
2272+
format!("{} ", comment)
2273+
};
2274+
CommentAfterColon {
2275+
content: Some(content),
2276+
reaches_end_of_line,
2277+
}
2278+
},
2279+
);
22502280
(comment_before_colon, comment_after_colon)
22512281
}
22522282

@@ -2297,9 +2327,21 @@ impl Rewrite for ast::Param {
22972327
if !is_empty_infer(&*self.ty, self.pat.span) {
22982328
let (before_comment, after_comment) =
22992329
get_missing_param_comments(context, self.pat.span, self.ty.span, shape);
2300-
result.push_str(&before_comment);
2301-
result.push_str(colon_spaces(context.config));
2302-
result.push_str(&after_comment);
2330+
2331+
// In the specific case of comment after line reaching the end of that line,
2332+
// put the comment on-top to keep it from obscuring the following type
2333+
if after_comment.reaches_end_of_line {
2334+
let mut reordered = after_comment.content().to_string();
2335+
reordered.push_str(&shape.indent.to_string_with_newline(context.config));
2336+
reordered.push_str(&result);
2337+
result = reordered;
2338+
result.push_str(&before_comment);
2339+
result.push_str(colon_spaces(context.config));
2340+
} else {
2341+
result.push_str(&before_comment);
2342+
result.push_str(colon_spaces(context.config));
2343+
result.push_str(after_comment.content());
2344+
}
23032345
let overhead = last_line_width(&result);
23042346
let max_width = shape
23052347
.width
@@ -2327,7 +2369,7 @@ impl Rewrite for ast::Param {
23272369
)?;
23282370
result.push_str(&before_comment);
23292371
result.push_str(colon_spaces(context.config));
2330-
result.push_str(&after_comment);
2372+
result.push_str(after_comment.content());
23312373
let overhead = last_line_width(&result);
23322374
let max_width = shape
23332375
.width

tests/source/issue_6520.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pub fn foo(x: // comment here
2+
u32) {}
3+
4+
pub fn bar(x: // comment here
5+
u32, y: i32) {}
6+
7+
pub fn baz(
8+
my_arg1: i32,
9+
my_arg2: u32,
10+
my_arg3: bool,
11+
my_arg4: // other comment here
12+
f32,
13+
my_arg5: String,
14+
my_arg6: u32,
15+
my_arg7: bool,
16+
) {
17+
}

tests/target/issue_6520.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pub fn foo(
2+
// comment here
3+
x: u32,
4+
) {
5+
}
6+
7+
pub fn bar(
8+
// comment here
9+
x: u32,
10+
y: i32,
11+
) {
12+
}
13+
14+
pub fn baz(
15+
my_arg1: i32,
16+
my_arg2: u32,
17+
my_arg3: bool,
18+
// other comment here
19+
my_arg4: f32,
20+
my_arg5: String,
21+
my_arg6: u32,
22+
my_arg7: bool,
23+
) {
24+
}

0 commit comments

Comments
 (0)