Skip to content

Commit 65f41f3

Browse files
ChinYing-Licalebcartwright
authored andcommitted
Filter out block comment in filter_normal_code (#4668)
Add idempotent tests for wrapping block-style comment
1 parent 87e9602 commit 65f41f3

File tree

7 files changed

+142
-2
lines changed

7 files changed

+142
-2
lines changed

Diff for: src/formatting/comment.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,9 @@ impl<'a> Iterator for LineClasses<'a> {
14221422
None => unreachable!(),
14231423
};
14241424

1425+
let mut prev_kind = FullCodeCharKind::Normal;
14251426
while let Some((kind, c)) = self.base.next() {
1427+
prev_kind = self.kind;
14261428
// needed to set the kind of the ending character on the last line
14271429
self.kind = kind;
14281430
if c == '\n' {
@@ -1439,6 +1441,9 @@ impl<'a> Iterator for LineClasses<'a> {
14391441
(FullCodeCharKind::InStringCommented, FullCodeCharKind::InComment) => {
14401442
FullCodeCharKind::EndStringCommented
14411443
}
1444+
(_, FullCodeCharKind::Normal) if prev_kind == FullCodeCharKind::EndComment => {
1445+
FullCodeCharKind::EndComment
1446+
}
14421447
_ => kind,
14431448
};
14441449
break;
@@ -1993,12 +1998,28 @@ fn main() {
19931998
}
19941999
"#;
19952000
assert_eq!(s, filter_normal_code(s));
1996-
let s_with_comment = r#"
2001+
let s_with_line_comment = r#"
19972002
fn main() {
19982003
// hello, world
19992004
println!("hello, world");
20002005
}
20012006
"#;
2002-
assert_eq!(s, filter_normal_code(s_with_comment));
2007+
assert_eq!(s, filter_normal_code(s_with_line_comment));
2008+
let s_with_block_comment = r#"
2009+
fn main() {
2010+
/* hello, world */
2011+
println!("hello, world");
2012+
}
2013+
"#;
2014+
assert_eq!(s, filter_normal_code(s_with_block_comment));
2015+
let s_with_multi_line_comment = r#"
2016+
fn main() {
2017+
/* hello,
2018+
* world
2019+
*/
2020+
println!("hello, world");
2021+
}
2022+
"#;
2023+
assert_eq!(s, filter_normal_code(s_with_multi_line_comment));
20032024
}
20042025
}

Diff for: tests/source/comment2.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22

33
/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly and justly.
44
pub mod foo {}
5+
6+
fn chains() {
7+
test().map(|| {
8+
let x = 11;
9+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue
10+
* ligula ac quam */ x
11+
});
12+
}

Diff for: tests/source/comment4.rs

+43
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,46 @@ fn debug_function() {
5050
#[no_mangle] // Test this attribute is preserved.
5151
#[cfg_attr(rustfmt, rustfmt::skip)]
5252
pub static ISSUE_1284: [i32; 16] = [];
53+
54+
// issue 4668
55+
fn f() {
56+
fn g() -> int {
57+
let foo = 12;
58+
match foo {
59+
0..=10 => {
60+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex. */
61+
}
62+
_ => {
63+
let st = None;
64+
let res = st.unwrap_or(
65+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex. */ "abc"
66+
);
67+
}
68+
}
69+
}
70+
}
71+
72+
fn h() {
73+
let mut y = 0;
74+
while let x = vec![1, 2, 3]
75+
.iter()
76+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */
77+
.take(3)
78+
.skip(1)
79+
.to_owned()
80+
.next()
81+
{
82+
y += x.unwrap_or_else(|/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */_| &0) * 2;
83+
}
84+
}
85+
86+
pub fn t(x: int) {
87+
if x == 3 {
88+
let y = match context {
89+
True => {
90+
|x| /* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */ x+2
91+
}
92+
False => |x| x,
93+
};
94+
} else {}
95+
}

Diff for: tests/source/issue-4668.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const C: usize = 0_usize;
2+
const U: usize = /* A long block-style comment A long block-style comment A long block-style comment A long block-style comment */ if C > 0 { 4 } else { 3 };

Diff for: tests/target/comment2.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@
33
/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly
44
/// and justly.
55
pub mod foo {}
6+
7+
fn chains() {
8+
test().map(|| {
9+
let x = 11;
10+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam
11+
* lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam */
12+
x
13+
});
14+
}

Diff for: tests/target/comment4.rs

+53
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,56 @@ fn debug_function() {
4949
#[no_mangle] // Test this attribute is preserved.
5050
#[cfg_attr(rustfmt, rustfmt::skip)]
5151
pub static ISSUE_1284: [i32; 16] = [];
52+
53+
// issue 4668
54+
fn f() {
55+
fn g() -> int {
56+
let foo = 12;
57+
match foo {
58+
0..=10 => {
59+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex. */
60+
}
61+
_ => {
62+
let st = None;
63+
let res = st.unwrap_or(
64+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex. */
65+
"abc",
66+
);
67+
}
68+
}
69+
}
70+
}
71+
72+
fn h() {
73+
let mut y = 0;
74+
while let x = vec![1, 2, 3]
75+
.iter()
76+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */
77+
.take(3)
78+
.skip(1)
79+
.to_owned()
80+
.next()
81+
{
82+
y += x.unwrap_or_else(
83+
|
84+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */
85+
_,
86+
| &0,
87+
) * 2;
88+
}
89+
}
90+
91+
pub fn t(x: int) {
92+
if x == 3 {
93+
let y = match context {
94+
True => {
95+
|x| {
96+
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */
97+
x + 2
98+
}
99+
}
100+
False => |x| x,
101+
};
102+
} else {
103+
}
104+
}

Diff for: tests/target/issue-4668.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const C: usize = 0_usize;
2+
const U: usize =
3+
/* A long block-style comment A long block-style comment A long block-style comment A long block-style comment */
4+
if C > 0 { 4 } else { 3 };

0 commit comments

Comments
 (0)