]> git.lizzy.rs Git - rust.git/commitdiff
Use map + sum instead of fold for computing Unicode width
authorAnthonyMikh <anthony.mikh@yandex.ru>
Wed, 25 Sep 2019 20:23:19 +0000 (23:23 +0300)
committerGitHub <noreply@github.com>
Wed, 25 Sep 2019 20:23:19 +0000 (23:23 +0300)
src/librustc_errors/emitter.rs

index 151c06ddbbd4c732a4b5626fe245069d1eb39d32..17a5fac6da75ea77a97a6d19677286b87dacc38a 100644 (file)
@@ -594,9 +594,9 @@ fn render_source_line(
 
         let left = margin.left(source_string.len()); // Left trim
         // Account for unicode characters of width !=0 that were removed.
-        let left = source_string.chars().take(left).fold(0, |acc, ch| {
-            acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1)
-        });
+        let left = source_string.chars().take(left)
+            .map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
+            .sum();
 
         self.draw_line(
             buffer,
@@ -1512,9 +1512,9 @@ fn emit_suggestion_default(
                             .saturating_sub(part.snippet.trim_start().len());
                         // ...or trailing spaces. Account for substitutions containing unicode
                         // characters.
-                        let sub_len = part.snippet.trim().chars().fold(0, |acc, ch| {
-                            acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1)
-                        });
+                        let sub_len = part.snippet.trim().chars()
+                            .map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
+                            .sum();
 
                         let underline_start = (span_start_pos + start) as isize + offset;
                         let underline_end = (span_start_pos + start + sub_len) as isize + offset;
@@ -1535,9 +1535,9 @@ fn emit_suggestion_default(
                         }
 
                         // length of the code after substitution
-                        let full_sub_len = part.snippet.chars().fold(0, |acc, ch| {
-                            acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) as isize
-                        });
+                        let full_sub_len = part.snippet.chars()
+                            .map(|ch| acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
+                            .sum() as isize;
 
                         // length of the code to be substituted
                         let snippet_len = span_end_pos as isize - span_start_pos as isize;