Skip to content

Issue 4668 #4720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/formatting/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,9 @@ impl<'a> Iterator for LineClasses<'a> {
None => unreachable!(),
};

let mut prev_kind = FullCodeCharKind::Normal;
while let Some((kind, c)) = self.base.next() {
prev_kind = self.kind;
// needed to set the kind of the ending character on the last line
self.kind = kind;
if c == '\n' {
Expand All @@ -1439,6 +1441,9 @@ impl<'a> Iterator for LineClasses<'a> {
(FullCodeCharKind::InStringCommented, FullCodeCharKind::InComment) => {
FullCodeCharKind::EndStringCommented
}
(_, FullCodeCharKind::Normal) if prev_kind == FullCodeCharKind::EndComment => {
FullCodeCharKind::EndComment
}
_ => kind,
};
break;
Expand Down Expand Up @@ -1993,12 +1998,28 @@ fn main() {
}
"#;
assert_eq!(s, filter_normal_code(s));
let s_with_comment = r#"
let s_with_line_comment = r#"
fn main() {
// hello, world
println!("hello, world");
}
"#;
assert_eq!(s, filter_normal_code(s_with_comment));
assert_eq!(s, filter_normal_code(s_with_line_comment));
let s_with_block_comment = r#"
fn main() {
/* hello, world */
println!("hello, world");
}
"#;
assert_eq!(s, filter_normal_code(s_with_block_comment));
let s_with_multi_line_comment = r#"
fn main() {
/* hello,
* world
*/
println!("hello, world");
}
"#;
assert_eq!(s, filter_normal_code(s_with_multi_line_comment));
}
}
8 changes: 8 additions & 0 deletions tests/source/comment2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@

/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly and justly.
pub mod foo {}

fn chains() {
test().map(|| {
let x = 11;
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue
* ligula ac quam */ x
});
}
43 changes: 43 additions & 0 deletions tests/source/comment4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,46 @@ fn debug_function() {
#[no_mangle] // Test this attribute is preserved.
#[cfg_attr(rustfmt, rustfmt::skip)]
pub static ISSUE_1284: [i32; 16] = [];

// issue 4668
fn f() {
fn g() -> int {
let foo = 12;
match foo {
0..=10 => {
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex. */
}
_ => {
let st = None;
let res = st.unwrap_or(
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex. */ "abc"
);
}
}
}
}

fn h() {
let mut y = 0;
while let x = vec![1, 2, 3]
.iter()
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */
.take(3)
.skip(1)
.to_owned()
.next()
{
y += x.unwrap_or_else(|/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */_| &0) * 2;
}
}

pub fn t(x: int) {
if x == 3 {
let y = match context {
True => {
|x| /* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */ x+2
}
False => |x| x,
};
} else {}
}
2 changes: 2 additions & 0 deletions tests/source/issue-4668.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const C: usize = 0_usize;
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 };
9 changes: 9 additions & 0 deletions tests/target/comment2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly
/// and justly.
pub mod foo {}

fn chains() {
test().map(|| {
let x = 11;
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam
* lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam */
x
});
}
53 changes: 53 additions & 0 deletions tests/target/comment4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,56 @@ fn debug_function() {
#[no_mangle] // Test this attribute is preserved.
#[cfg_attr(rustfmt, rustfmt::skip)]
pub static ISSUE_1284: [i32; 16] = [];

// issue 4668
fn f() {
fn g() -> int {
let foo = 12;
match foo {
0..=10 => {
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex. */
}
_ => {
let st = None;
let res = st.unwrap_or(
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex. */
"abc",
);
}
}
}
}

fn h() {
let mut y = 0;
while let x = vec![1, 2, 3]
.iter()
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */
.take(3)
.skip(1)
.to_owned()
.next()
{
y += x.unwrap_or_else(
|
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */
_,
| &0,
) * 2;
}
}

pub fn t(x: int) {
if x == 3 {
let y = match context {
True => {
|x| {
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna felis, molestie ex... */
x + 2
}
}
False => |x| x,
};
} else {
}
}
4 changes: 4 additions & 0 deletions tests/target/issue-4668.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const C: usize = 0_usize;
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 };