Skip to content

Commit 54da77a

Browse files
committed
trailing newlines in post_comments should be removed
1 parent 3212d75 commit 54da77a

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

src/lists.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -644,19 +644,28 @@ pub(crate) fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListIte
644644
}
645645

646646
fn extract_post_comment(post_snippet: &str, comment_end: usize, separator: &str) -> Option<String> {
647+
// leading newlines are important but not when they are trailing
647648
let white_space: &[_] = &[' ', '\t'];
648649

649650
// Cleanup post-comment: strip separators and whitespace.
650651
let post_snippet = post_snippet[..comment_end].trim();
651652
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
652-
post_snippet[1..].trim_matches(white_space)
653+
post_snippet[1..]
654+
.trim_matches(white_space)
655+
.trim_end_matches('\n')
653656
} else if post_snippet.ends_with(separator) {
654657
// the separator is in front of the next item
655-
post_snippet[..post_snippet.len() - separator.len()].trim_matches(white_space)
658+
post_snippet[..post_snippet.len() - separator.len()]
659+
.trim_matches(white_space)
660+
.trim_end_matches('\n')
656661
} else if post_snippet.starts_with(separator) {
657-
post_snippet[separator.len()..].trim_matches(white_space)
662+
post_snippet[separator.len()..]
663+
.trim_matches(white_space)
664+
.trim_end_matches('\n')
658665
} else if post_snippet.ends_with(',') && !post_snippet.trim_start().starts_with("//") {
659-
post_snippet[..post_snippet.len() - 1].trim_matches(white_space)
666+
post_snippet[..post_snippet.len() - 1]
667+
.trim_matches(white_space)
668+
.trim_end_matches('\n')
660669
} else {
661670
post_snippet
662671
};
@@ -990,7 +999,7 @@ mod test {
990999
"// a comment\n +",
9911000
"// a comment\n +".len(),
9921001
"+",
993-
"// a comment\n",
1002+
"// a comment",
9941003
),
9951004
(
9961005
"+ // a comment\n ",

tests/source/issue-3669.rs

+25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ pub trait PCG: self::sealed::Sealed // comment1
2323
+ for<'a> Deserialize<'a>; // Note(Evrey): Because Rust is drunk. 13
2424
}
2525

26+
pub trait Bar: self::sealed::Sealed + // comment1
27+
Sized + // comment2
28+
Eq + // comment3
29+
Hash + // comment4
30+
Debug + // comment5
31+
Clone + // comment6
32+
Default + // comment7
33+
Serialize + // comment8
34+
for<'a> Deserialize<'a> // comment9
35+
{
36+
type DoubleState: Copy + // Note(Evrey): Because Rust is drunk. 1
37+
ShrAssign<u8> + // Note(Evrey): Because Rust is drunk. 2
38+
Shl<u8, Output = Self::DoubleState> + // Note(Evrey): Because Rust is drunk. 3
39+
BitAnd<Output = Self::DoubleState> + // Note(Evrey): Because Rust is drunk. 4
40+
BitOrAssign + // Note(Evrey): Because Rust is drunk. 5
41+
Sub<Output = Self::DoubleState> + // Note(Evrey): Because Rust is drunk. 6
42+
Into<u128> + // Note(Evrey): Because Rust is drunk. 7
43+
Debug + // Note(Evrey): Because Rust is drunk. 8
44+
Eq + // Note(Evrey): Because Rust is drunk. 9
45+
Hash + // Note(Evrey): Because Rust is drunk. 10
46+
Default + // Note(Evrey): Because Rust is drunk. 11
47+
Serialize + // Note(Evrey): Because Rust is drunk. 12
48+
for<'a> Deserialize<'a>; // Note(Evrey): Because Rust is drunk. 13
49+
}
50+
2651
pub trait Foo: self::sealed::Sealed
2752
+ Sized
2853
+ Eq

tests/target/issue-3669.rs

+37-11
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,44 @@ pub trait PCG:
99
+ Serialize // comment8
1010
+ for<'a> Deserialize<'a> // comment9
1111
{
12-
type DoubleState: Copy // Note(Evrey): Because Rust is drunk. 1
13-
+ ShrAssign<u8> // Note(Evrey): Because Rust is drunk. 2
12+
type DoubleState: Copy // Note(Evrey): Because Rust is drunk. 1
13+
+ ShrAssign<u8> // Note(Evrey): Because Rust is drunk. 2
1414
+ Shl<u8, Output = Self::DoubleState> // Note(Evrey): Because Rust is drunk. 3
15-
+ BitAnd<Output = Self::DoubleState> // Note(Evrey): Because Rust is drunk. 4
16-
+ BitOrAssign // Note(Evrey): Because Rust is drunk. 5
17-
+ Sub<Output = Self::DoubleState> // Note(Evrey): Because Rust is drunk. 6
18-
+ Into<u128> // Note(Evrey): Because Rust is drunk. 7
19-
+ Debug // Note(Evrey): Because Rust is drunk. 8
20-
+ Eq // Note(Evrey): Because Rust is drunk. 9
21-
+ Hash // Note(Evrey): Because Rust is drunk. 10
22-
+ Default // Note(Evrey): Because Rust is drunk. 11
23-
+ Serialize // Note(Evrey): Because Rust is drunk. 12
15+
+ BitAnd<Output = Self::DoubleState> // Note(Evrey): Because Rust is drunk. 4
16+
+ BitOrAssign // Note(Evrey): Because Rust is drunk. 5
17+
+ Sub<Output = Self::DoubleState> // Note(Evrey): Because Rust is drunk. 6
18+
+ Into<u128> // Note(Evrey): Because Rust is drunk. 7
19+
+ Debug // Note(Evrey): Because Rust is drunk. 8
20+
+ Eq // Note(Evrey): Because Rust is drunk. 9
21+
+ Hash // Note(Evrey): Because Rust is drunk. 10
22+
+ Default // Note(Evrey): Because Rust is drunk. 11
23+
+ Serialize // Note(Evrey): Because Rust is drunk. 12
24+
+ for<'a> Deserialize<'a>; // Note(Evrey): Because Rust is drunk. 13
25+
}
26+
27+
pub trait Bar:
28+
self::sealed::Sealed // comment1
29+
+ Sized // comment2
30+
+ Eq // comment3
31+
+ Hash // comment4
32+
+ Debug // comment5
33+
+ Clone // comment6
34+
+ Default // comment7
35+
+ Serialize // comment8
36+
+ for<'a> Deserialize<'a> // comment9
37+
{
38+
type DoubleState: Copy // Note(Evrey): Because Rust is drunk. 1
39+
+ ShrAssign<u8> // Note(Evrey): Because Rust is drunk. 2
40+
+ Shl<u8, Output = Self::DoubleState> // Note(Evrey): Because Rust is drunk. 3
41+
+ BitAnd<Output = Self::DoubleState> // Note(Evrey): Because Rust is drunk. 4
42+
+ BitOrAssign // Note(Evrey): Because Rust is drunk. 5
43+
+ Sub<Output = Self::DoubleState> // Note(Evrey): Because Rust is drunk. 6
44+
+ Into<u128> // Note(Evrey): Because Rust is drunk. 7
45+
+ Debug // Note(Evrey): Because Rust is drunk. 8
46+
+ Eq // Note(Evrey): Because Rust is drunk. 9
47+
+ Hash // Note(Evrey): Because Rust is drunk. 10
48+
+ Default // Note(Evrey): Because Rust is drunk. 11
49+
+ Serialize // Note(Evrey): Because Rust is drunk. 12
2450
+ for<'a> Deserialize<'a>; // Note(Evrey): Because Rust is drunk. 13
2551
}
2652

0 commit comments

Comments
 (0)