Skip to content

merge commits from rustfmt-2.0 branch #3907

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 3 commits into from
Nov 8, 2019
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
24 changes: 16 additions & 8 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,16 @@ impl Ord for UseSegment {
match (self, other) {
(&Slf(ref a), &Slf(ref b))
| (&Super(ref a), &Super(ref b))
| (&Crate(ref a), &Crate(ref b)) => a.cmp(b),
| (&Crate(ref a), &Crate(ref b)) => match (a, b) {
(Some(sa), Some(sb)) => {
sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#"))
}
(_, _) => a.cmp(b),
},
(&Glob, &Glob) => Ordering::Equal,
(&Ident(ref ia, ref aa), &Ident(ref ib, ref ab)) => {
(&Ident(ref pia, ref aa), &Ident(ref pib, ref ab)) => {
let ia = pia.trim_start_matches("r#");
let ib = pib.trim_start_matches("r#");
// snake_case < CamelCase < UPPER_SNAKE_CASE
if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
return Ordering::Greater;
Expand All @@ -682,13 +689,14 @@ impl Ord for UseSegment {
if ident_ord != Ordering::Equal {
return ident_ord;
}
if aa.is_none() && ab.is_some() {
return Ordering::Less;
}
if aa.is_some() && ab.is_none() {
return Ordering::Greater;
match (aa, ab) {
(None, Some(_)) => Ordering::Less,
(Some(_), None) => Ordering::Greater,
(Some(aas), Some(abs)) => aas
.trim_start_matches("r#")
.cmp(abs.trim_start_matches("r#")),
(None, None) => Ordering::Equal,
}
aa.cmp(ab)
}
(&List(ref a), &List(ref b)) => {
for (a, b) in a.iter().zip(b.iter()) {
Expand Down
22 changes: 19 additions & 3 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ fn rewrite_match_body(
arrow_span: Span,
is_last: bool,
) -> Option<String> {
let was_block = if let ast::ExprKind::Block(..) = body.kind {
true
} else {
false
};

let (extend, body) = flatten_arm_body(
context,
body,
Expand Down Expand Up @@ -368,14 +374,17 @@ fn rewrite_match_body(
let comment_str = arrow_snippet[arrow_index + 2..].trim();
if comment_str.is_empty() {
String::new()
} else if (!was_block && !is_block) || is_empty_block {
let indent = shape.indent.block_indent(context.config);
let shape = Shape::indented(indent, &context.config);
rewrite_comment(comment_str, false, shape, &context.config)?
} else {
rewrite_comment(comment_str, false, shape, &context.config)?
}
};

let combine_next_line_body = |body_str: &str| {
let nested_indent_str = next_line_indent.to_string_with_newline(context.config);

if is_block {
let mut result = pats_str.to_owned();
result.push_str(" =>");
Expand Down Expand Up @@ -413,19 +422,26 @@ fn rewrite_match_body(
let block_sep = match context.config.control_brace_style() {
ControlBraceStyle::AlwaysNextLine => format!("{}{}", alt_block_sep, body_prefix),
_ if body_prefix.is_empty() => "".to_owned(),
_ if forbid_same_line || !arrow_comment.is_empty() => {
_ if forbid_same_line || (!arrow_comment.is_empty() && was_block) => {
format!("{}{}", alt_block_sep, body_prefix)
}
_ => format!(" {}", body_prefix),
} + &nested_indent_str;

// if match arm was a block consisting of one expression,
// and it was flattened, we need to retain comment before
// the arm body block.
let mut result = pats_str.to_owned();
result.push_str(" =>");
if !arrow_comment.is_empty() {
if !arrow_comment.is_empty() && was_block {
result.push_str(&indent_str);
result.push_str(&arrow_comment);
}
result.push_str(&block_sep);
if !arrow_comment.is_empty() && !was_block {
result.push_str(&arrow_comment);
result.push_str(&nested_indent_str);
}
result.push_str(&body_str);
result.push_str(&body_suffix);
Some(result)
Expand Down
7 changes: 7 additions & 0 deletions tests/source/imports_raw_identifiers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use websocket::client::ClientBuilder;
use websocket::r#async::futures::Stream;
use websocket::result::WebSocketError;

fn main() {
println!("Hello, world!");
}
39 changes: 39 additions & 0 deletions tests/source/issue-3170.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
fn main() {
let n = 1;
match n {
1 =>
// comment 1.1
// comment 1.2
1,
2 => // comment 2.1
// comment 2.2
{
// comment 2.3
// comment 2.4
2
}
3 =>
// comment 3.1
// comment 3.2
{
3
},
4 =>
{
// comment 4.1
// comment 4.2
4
}
5 =>
5,
6 =>
{ // comment 6
}
7 => // comment 7
{}
8 =>
{
// comment 8
}
};
}
7 changes: 7 additions & 0 deletions tests/target/imports_raw_identifiers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use websocket::r#async::futures::Stream;
use websocket::client::ClientBuilder;
use websocket::result::WebSocketError;

fn main() {
println!("Hello, world!");
}
38 changes: 38 additions & 0 deletions tests/target/issue-3170.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
fn main() {
let n = 1;
match n {
1 => {
// comment 1.1
// comment 1.2
1
}
2 =>
// comment 2.1
// comment 2.2
{
// comment 2.3
// comment 2.4
2
}
3 =>
// comment 3.1
// comment 3.2
{
3
}
4 => {
// comment 4.1
// comment 4.2
4
}
5 => 5,
6 => { // comment 6
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did have to update this one line to get the tests to pass as the comment is now being kept on the same line as the opening bracket. IIRC that's because been some subequent updates that maintain that same line association. I added another arm below that I believe covers that original use case

7 =>
// comment 7
{}
8 => {
// comment 8
}
};
}