Skip to content

Commit b305d62

Browse files
fix: correct arm leading pipe check (rust-lang#4880)
In the event a pattern starts with a leading pipe the pattern span will contain, and begin with, the pipe. This updates the process to see if a match arm contains a leading pipe by leveraging this recent(ish) change to the patterns in the AST, and avoids an indexing bug that occurs when a pattern starts with a non-ascii char in the old implementation.
1 parent e634a6f commit b305d62

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

src/matches.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::source_map::SpanUtils;
1919
use crate::spanned::Spanned;
2020
use crate::utils::{
2121
contains_skip, extra_offset, first_line_width, inner_attributes, last_line_extendable, mk_sp,
22-
mk_sp_lo_plus_one, semicolon_for_expr, trimmed_last_line_width, unicode_str_width,
22+
semicolon_for_expr, trimmed_last_line_width, unicode_str_width,
2323
};
2424

2525
/// A simple wrapper type against `ast::Arm`. Used inside `write_list()`.
@@ -167,8 +167,9 @@ fn collect_beginning_verts(
167167
arms.iter()
168168
.map(|a| {
169169
context
170-
.snippet_provider
171-
.opt_span_before(mk_sp_lo_plus_one(a.pat.span.lo()), "|")
170+
.snippet(a.pat.span)
171+
.starts_with("|")
172+
.then(|| a.pat.span().lo())
172173
})
173174
.collect()
174175
}

tests/source/configs/match_arm_leading_pipes/preserve.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ fn bar() {
2626
_ => {}
2727
}
2828
}
29+
30+
fn f(x: NonAscii) -> bool {
31+
match x {
32+
// foo
33+
| Éfgh => true,
34+
_ => false,
35+
}
36+
}

tests/target/configs/match_arm_leading_pipes/preserve.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ fn bar() {
2525
_ => {}
2626
}
2727
}
28+
29+
fn f(x: NonAscii) -> bool {
30+
match x {
31+
// foo
32+
| Éfgh => true,
33+
_ => false,
34+
}
35+
}

tests/target/issue_4868.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
enum NonAscii {
2+
Abcd,
3+
Éfgh,
4+
}
5+
6+
use NonAscii::*;
7+
8+
fn f(x: NonAscii) -> bool {
9+
match x {
10+
Éfgh => true,
11+
_ => false,
12+
}
13+
}
14+
15+
fn main() {
16+
dbg!(f(Abcd));
17+
}

0 commit comments

Comments
 (0)