about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCaleb Cartwright <calebcartwright@users.noreply.github.com>2021-06-24 18:22:28 -0500
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2021-07-25 22:53:32 -0500
commitb305d62e5b9e08c6f4540de0a349fbf6da3dc0e4 (patch)
tree91c0b7b4d05f417567592f027bb63ef433d5f5b2
parente634a6f9a8ad9435f2d9f6bbfb6a8eb018ee8e3f (diff)
downloadrust-b305d62e5b9e08c6f4540de0a349fbf6da3dc0e4.tar.gz
rust-b305d62e5b9e08c6f4540de0a349fbf6da3dc0e4.zip
fix: correct arm leading pipe check (#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.
-rw-r--r--src/matches.rs7
-rw-r--r--tests/source/configs/match_arm_leading_pipes/preserve.rs8
-rw-r--r--tests/target/configs/match_arm_leading_pipes/preserve.rs8
-rw-r--r--tests/target/issue_4868.rs17
4 files changed, 37 insertions, 3 deletions
diff --git a/src/matches.rs b/src/matches.rs
index f33fedce92d..140ec226c02 100644
--- a/src/matches.rs
+++ b/src/matches.rs
@@ -19,7 +19,7 @@ use crate::source_map::SpanUtils;
 use crate::spanned::Spanned;
 use crate::utils::{
     contains_skip, extra_offset, first_line_width, inner_attributes, last_line_extendable, mk_sp,
-    mk_sp_lo_plus_one, semicolon_for_expr, trimmed_last_line_width, unicode_str_width,
+    semicolon_for_expr, trimmed_last_line_width, unicode_str_width,
 };
 
 /// A simple wrapper type against `ast::Arm`. Used inside `write_list()`.
@@ -167,8 +167,9 @@ fn collect_beginning_verts(
     arms.iter()
         .map(|a| {
             context
-                .snippet_provider
-                .opt_span_before(mk_sp_lo_plus_one(a.pat.span.lo()), "|")
+                .snippet(a.pat.span)
+                .starts_with("|")
+                .then(|| a.pat.span().lo())
         })
         .collect()
 }
diff --git a/tests/source/configs/match_arm_leading_pipes/preserve.rs b/tests/source/configs/match_arm_leading_pipes/preserve.rs
index ea303e857de..5486877bde1 100644
--- a/tests/source/configs/match_arm_leading_pipes/preserve.rs
+++ b/tests/source/configs/match_arm_leading_pipes/preserve.rs
@@ -26,3 +26,11 @@ fn bar() {
         _ => {}
     }
 }
+
+fn f(x: NonAscii) -> bool {
+    match x {
+      // foo
+              |   Éfgh => true,
+        _ => false,
+    }
+}
\ No newline at end of file
diff --git a/tests/target/configs/match_arm_leading_pipes/preserve.rs b/tests/target/configs/match_arm_leading_pipes/preserve.rs
index 2beb1f5d813..4775575842a 100644
--- a/tests/target/configs/match_arm_leading_pipes/preserve.rs
+++ b/tests/target/configs/match_arm_leading_pipes/preserve.rs
@@ -25,3 +25,11 @@ fn bar() {
         _ => {}
     }
 }
+
+fn f(x: NonAscii) -> bool {
+    match x {
+        // foo
+        | Éfgh => true,
+        _ => false,
+    }
+}
diff --git a/tests/target/issue_4868.rs b/tests/target/issue_4868.rs
new file mode 100644
index 00000000000..763a82c3231
--- /dev/null
+++ b/tests/target/issue_4868.rs
@@ -0,0 +1,17 @@
+enum NonAscii {
+    Abcd,
+    Éfgh,
+}
+
+use NonAscii::*;
+
+fn f(x: NonAscii) -> bool {
+    match x {
+        Éfgh => true,
+        _ => false,
+    }
+}
+
+fn main() {
+    dbg!(f(Abcd));
+}