@@ -11,7 +11,7 @@ use rustc_ast as ast;
11
11
use rustc_span:: symbol:: Ident ;
12
12
use rustc_span:: Span ;
13
13
14
- use crate :: comment:: combine_strs_with_missing_comments;
14
+ use crate :: comment:: { combine_strs_with_missing_comments, contains_comment } ;
15
15
use crate :: rewrite:: RewriteContext ;
16
16
use crate :: shape:: Shape ;
17
17
use crate :: utils:: rewrite_ident;
@@ -24,6 +24,13 @@ pub(crate) fn format_header(
24
24
debug ! ( ?parts, "format_header" ) ;
25
25
let shape = shape. infinite_width ( ) ;
26
26
27
+ // FIXME(ytmimi) Don't apply any formatting if the header contains comments.
28
+ // the rustfmt team needs to decide how to handle comments within the header
29
+ if comments_in_header ( context, & parts) {
30
+ let span = header_span ( & parts) . expect ( "If we have comments we have headers" ) ;
31
+ return context. snippet ( span) . to_owned ( ) ;
32
+ }
33
+
27
34
// Empty `HeaderPart`s are ignored.
28
35
let mut parts = parts. into_iter ( ) . filter ( |x| !x. snippet . is_empty ( ) ) ;
29
36
let Some ( part) = parts. next ( ) else {
@@ -51,6 +58,23 @@ pub(crate) fn format_header(
51
58
result
52
59
}
53
60
61
+ /// Get the Span of the entire header
62
+ fn header_span ( parts : & [ HeaderPart ] ) -> Option < Span > {
63
+ let first = parts. first ( ) ?;
64
+ let last = parts. last ( ) ?;
65
+ Some ( first. span . with_hi ( last. span . hi ( ) ) )
66
+ }
67
+
68
+ /// Check if there are any comments in the header parts
69
+ fn comments_in_header ( context : & RewriteContext < ' _ > , parts : & [ HeaderPart ] ) -> bool {
70
+ let Some ( span) = header_span ( parts) else {
71
+ return false ;
72
+ } ;
73
+
74
+ let snippet = context. snippet ( span) ;
75
+ contains_comment ( snippet)
76
+ }
77
+
54
78
#[ derive( Debug ) ]
55
79
pub ( crate ) struct HeaderPart {
56
80
/// snippet of this part without surrounding space
0 commit comments