Skip to content

Commit 7037481

Browse files
calebcartwrighttopecongiro
authored andcommitted
merge commits from rustfmt-2.0 branch (#3907)
1 parent fa73384 commit 7037481

File tree

6 files changed

+126
-11
lines changed

6 files changed

+126
-11
lines changed

src/imports.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,16 @@ impl Ord for UseSegment {
662662
match (self, other) {
663663
(&Slf(ref a), &Slf(ref b))
664664
| (&Super(ref a), &Super(ref b))
665-
| (&Crate(ref a), &Crate(ref b)) => a.cmp(b),
665+
| (&Crate(ref a), &Crate(ref b)) => match (a, b) {
666+
(Some(sa), Some(sb)) => {
667+
sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#"))
668+
}
669+
(_, _) => a.cmp(b),
670+
},
666671
(&Glob, &Glob) => Ordering::Equal,
667-
(&Ident(ref ia, ref aa), &Ident(ref ib, ref ab)) => {
672+
(&Ident(ref pia, ref aa), &Ident(ref pib, ref ab)) => {
673+
let ia = pia.trim_start_matches("r#");
674+
let ib = pib.trim_start_matches("r#");
668675
// snake_case < CamelCase < UPPER_SNAKE_CASE
669676
if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
670677
return Ordering::Greater;
@@ -682,13 +689,14 @@ impl Ord for UseSegment {
682689
if ident_ord != Ordering::Equal {
683690
return ident_ord;
684691
}
685-
if aa.is_none() && ab.is_some() {
686-
return Ordering::Less;
687-
}
688-
if aa.is_some() && ab.is_none() {
689-
return Ordering::Greater;
692+
match (aa, ab) {
693+
(None, Some(_)) => Ordering::Less,
694+
(Some(_), None) => Ordering::Greater,
695+
(Some(aas), Some(abs)) => aas
696+
.trim_start_matches("r#")
697+
.cmp(abs.trim_start_matches("r#")),
698+
(None, None) => Ordering::Equal,
690699
}
691-
aa.cmp(ab)
692700
}
693701
(&List(ref a), &List(ref b)) => {
694702
for (a, b) in a.iter().zip(b.iter()) {

src/matches.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ fn rewrite_match_body(
326326
arrow_span: Span,
327327
is_last: bool,
328328
) -> Option<String> {
329+
let was_block = if let ast::ExprKind::Block(..) = body.kind {
330+
true
331+
} else {
332+
false
333+
};
334+
329335
let (extend, body) = flatten_arm_body(
330336
context,
331337
body,
@@ -368,14 +374,17 @@ fn rewrite_match_body(
368374
let comment_str = arrow_snippet[arrow_index + 2..].trim();
369375
if comment_str.is_empty() {
370376
String::new()
377+
} else if (!was_block && !is_block) || is_empty_block {
378+
let indent = shape.indent.block_indent(context.config);
379+
let shape = Shape::indented(indent, &context.config);
380+
rewrite_comment(comment_str, false, shape, &context.config)?
371381
} else {
372382
rewrite_comment(comment_str, false, shape, &context.config)?
373383
}
374384
};
375385

376386
let combine_next_line_body = |body_str: &str| {
377387
let nested_indent_str = next_line_indent.to_string_with_newline(context.config);
378-
379388
if is_block {
380389
let mut result = pats_str.to_owned();
381390
result.push_str(" =>");
@@ -413,19 +422,26 @@ fn rewrite_match_body(
413422
let block_sep = match context.config.control_brace_style() {
414423
ControlBraceStyle::AlwaysNextLine => format!("{}{}", alt_block_sep, body_prefix),
415424
_ if body_prefix.is_empty() => "".to_owned(),
416-
_ if forbid_same_line || !arrow_comment.is_empty() => {
425+
_ if forbid_same_line || (!arrow_comment.is_empty() && was_block) => {
417426
format!("{}{}", alt_block_sep, body_prefix)
418427
}
419428
_ => format!(" {}", body_prefix),
420429
} + &nested_indent_str;
421430

431+
// if match arm was a block consisting of one expression,
432+
// and it was flattened, we need to retain comment before
433+
// the arm body block.
422434
let mut result = pats_str.to_owned();
423435
result.push_str(" =>");
424-
if !arrow_comment.is_empty() {
436+
if !arrow_comment.is_empty() && was_block {
425437
result.push_str(&indent_str);
426438
result.push_str(&arrow_comment);
427439
}
428440
result.push_str(&block_sep);
441+
if !arrow_comment.is_empty() && !was_block {
442+
result.push_str(&arrow_comment);
443+
result.push_str(&nested_indent_str);
444+
}
429445
result.push_str(&body_str);
430446
result.push_str(&body_suffix);
431447
Some(result)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use websocket::client::ClientBuilder;
2+
use websocket::r#async::futures::Stream;
3+
use websocket::result::WebSocketError;
4+
5+
fn main() {
6+
println!("Hello, world!");
7+
}

tests/source/issue-3170.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
fn main() {
2+
let n = 1;
3+
match n {
4+
1 =>
5+
// comment 1.1
6+
// comment 1.2
7+
1,
8+
2 => // comment 2.1
9+
// comment 2.2
10+
{
11+
// comment 2.3
12+
// comment 2.4
13+
2
14+
}
15+
3 =>
16+
// comment 3.1
17+
// comment 3.2
18+
{
19+
3
20+
},
21+
4 =>
22+
{
23+
// comment 4.1
24+
// comment 4.2
25+
4
26+
}
27+
5 =>
28+
5,
29+
6 =>
30+
{ // comment 6
31+
}
32+
7 => // comment 7
33+
{}
34+
8 =>
35+
{
36+
// comment 8
37+
}
38+
};
39+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use websocket::r#async::futures::Stream;
2+
use websocket::client::ClientBuilder;
3+
use websocket::result::WebSocketError;
4+
5+
fn main() {
6+
println!("Hello, world!");
7+
}

tests/target/issue-3170.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
fn main() {
2+
let n = 1;
3+
match n {
4+
1 => {
5+
// comment 1.1
6+
// comment 1.2
7+
1
8+
}
9+
2 =>
10+
// comment 2.1
11+
// comment 2.2
12+
{
13+
// comment 2.3
14+
// comment 2.4
15+
2
16+
}
17+
3 =>
18+
// comment 3.1
19+
// comment 3.2
20+
{
21+
3
22+
}
23+
4 => {
24+
// comment 4.1
25+
// comment 4.2
26+
4
27+
}
28+
5 => 5,
29+
6 => { // comment 6
30+
}
31+
7 =>
32+
// comment 7
33+
{}
34+
8 => {
35+
// comment 8
36+
}
37+
};
38+
}

0 commit comments

Comments
 (0)