about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPhilipp Hansch <dev@phansch.net>2021-04-06 06:59:10 +0200
committerPhilipp Hansch <dev@phansch.net>2021-04-06 07:20:55 +0200
commit1573d10325f286ffcbba290d27984ded9538a5c3 (patch)
treeccf42fb5e45c2a8d49084d655783ef40696ce08d
parente315437bd69d97c7e11efe6e74dbad1ddb9ca172 (diff)
downloadrust-1573d10325f286ffcbba290d27984ded9538a5c3.tar.gz
rust-1573d10325f286ffcbba290d27984ded9538a5c3.zip
tabs_in_doc_comments: Fix ICE due to char indexing
This is a quick-fix for an ICE in `tabs_in_doc_comments`. The problem
was that we we're indexing into possibly multi-byte characters, such as '位'.

More specifically `get_chunks_of_tabs` was returning indices into
multi-byte characters. Those were passed on to a `Span` creation that
then caused the ICE.

This fix makes sure that we don't return indices that point inside a
multi-byte character. *However*, we are still iterating over unicode
codepoints, not grapheme clusters. So a seemingly single character like y̆ ,
which actually consists of two codepoints, will probably still cause
incorrect spans in the output.
-rw-r--r--clippy_lints/src/tabs_in_doc_comments.rs28
-rw-r--r--tests/ui/crashes/ice-5835.rs8
-rw-r--r--tests/ui/crashes/ice-5835.stderr20
3 files changed, 45 insertions, 11 deletions
diff --git a/clippy_lints/src/tabs_in_doc_comments.rs b/clippy_lints/src/tabs_in_doc_comments.rs
index 88bd2feaadd..3f9692540f7 100644
--- a/clippy_lints/src/tabs_in_doc_comments.rs
+++ b/clippy_lints/src/tabs_in_doc_comments.rs
@@ -104,30 +104,29 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
     // tracker to decide if the last group of tabs is not closed by a non-tab character
     let mut is_active = false;
 
-    let chars_array: Vec<_> = the_str.chars().collect();
+    let char_indices: Vec<_> = the_str.char_indices().collect();
 
-    if chars_array == vec!['\t'] {
+    if char_indices.len() == 1 && char_indices.first().unwrap().1 == '\t' {
         return vec![(0, 1)];
     }
 
-    for (index, arr) in chars_array.windows(2).enumerate() {
-        let index = u32::try_from(index).expect(line_length_way_to_long);
-        match arr {
-            ['\t', '\t'] => {
+    for entry in char_indices.windows(2) {
+        match entry {
+            [(_, '\t'), (_, '\t')] => {
                 // either string starts with double tab, then we have to set it active,
                 // otherwise is_active is true anyway
                 is_active = true;
             },
-            [_, '\t'] => {
+            [(_, _), (index_b, '\t')] => {
                 // as ['\t', '\t'] is excluded, this has to be a start of a tab group,
                 // set indices accordingly
                 is_active = true;
-                current_start = index + 1;
+                current_start = *index_b as u32;
             },
-            ['\t', _] => {
+            [(_, '\t'), (index_b, _)] => {
                 // this now has to be an end of the group, hence we have to push a new tuple
                 is_active = false;
-                spans.push((current_start, index + 1));
+                spans.push((current_start, *index_b as u32));
             },
             _ => {},
         }
@@ -137,7 +136,7 @@ fn get_chunks_of_tabs(the_str: &str) -> Vec<(u32, u32)> {
     if is_active {
         spans.push((
             current_start,
-            u32::try_from(the_str.chars().count()).expect(line_length_way_to_long),
+            u32::try_from(char_indices.last().unwrap().0 + 1).expect(line_length_way_to_long),
         ));
     }
 
@@ -149,6 +148,13 @@ mod tests_for_get_chunks_of_tabs {
     use super::get_chunks_of_tabs;
 
     #[test]
+    fn test_unicode_han_string() {
+        let res = get_chunks_of_tabs(" 位\t");
+
+        assert_eq!(res, vec![(4, 5)]);
+    }
+
+    #[test]
     fn test_empty_string() {
         let res = get_chunks_of_tabs("");
 
diff --git a/tests/ui/crashes/ice-5835.rs b/tests/ui/crashes/ice-5835.rs
new file mode 100644
index 00000000000..209a5b1eb09
--- /dev/null
+++ b/tests/ui/crashes/ice-5835.rs
@@ -0,0 +1,8 @@
+#![rustfmt::skip]
+
+pub struct Foo {
+    /// 位	
+    pub bar: u8,
+}
+
+fn main() {}
diff --git a/tests/ui/crashes/ice-5835.stderr b/tests/ui/crashes/ice-5835.stderr
new file mode 100644
index 00000000000..e286bc580ad
--- /dev/null
+++ b/tests/ui/crashes/ice-5835.stderr
@@ -0,0 +1,20 @@
+error[E0658]: custom inner attributes are unstable
+  --> $DIR/ice-5835.rs:1:4
+   |
+LL | #![rustfmt::skip]
+   |    ^^^^^^^^^^^^^
+   |
+   = note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
+   = help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
+
+error: using tabs in doc comments is not recommended
+  --> $DIR/ice-5835.rs:4:10
+   |
+LL |     /// 位    
+   |           ^^^^ help: consider using four spaces per tab
+   |
+   = note: `-D clippy::tabs-in-doc-comments` implied by `-D warnings`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0658`.