]> git.lizzy.rs Git - rust.git/blobdiff - src/missed_spans.rs
set of clippy changes
[rust.git] / src / missed_spans.rs
index f5794c65c4c4b7d48ffd0a506a8d49312cc11152..ef047b3144ccc3ea10d42745859242436adf9293 100644 (file)
@@ -9,14 +9,13 @@
 // except according to those terms.
 
 use std::borrow::Cow;
-use std::iter::repeat;
 
-use syntax::codemap::{BytePos, FileName, Pos, Span};
+use syntax::source_map::{BytePos, Pos, Span};
 
-use codemap::LineRangeUtils;
 use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
-use config::WriteMode;
+use config::{EmitMode, FileName};
 use shape::{Indent, Shape};
+use source_map::LineRangeUtils;
 use utils::{count_newlines, last_line_width, mk_sp};
 use visitor::FmtVisitor;
 
@@ -44,9 +43,20 @@ fn output_at_start(&self) -> bool {
         self.buffer.is_empty()
     }
 
-    // TODO these format_missing methods are ugly. Refactor and add unit tests
-    // for the central whitespace stripping loop.
     pub fn format_missing(&mut self, end: BytePos) {
+        // HACK(topecongiro)
+        // We use `format_missing()` to extract a missing comment between a macro
+        // (or alike) and a trailing semicolon. Here we just try to avoid calling
+        // `format_missing_inner` in the common case where there is no such comment.
+        // This is a hack, ideally we should fix a possible bug in `format_missing_inner`
+        // or refactor `visit_mac` and `rewrite_macro`, but this should suffice to fix the
+        // issue (#2727).
+        let missing_snippet = self.snippet(mk_sp(self.last_pos, end));
+        if missing_snippet.trim() == ";" {
+            self.push_str(";");
+            self.last_pos = end;
+            return;
+        }
         self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
     }
 
@@ -87,13 +97,19 @@ fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(
         assert!(
             start < end,
             "Request to format inverted span: {:?} to {:?}",
-            self.codemap.lookup_char_pos(start),
-            self.codemap.lookup_char_pos(end)
+            self.source_map.lookup_char_pos(start),
+            self.source_map.lookup_char_pos(end)
         );
 
         self.last_pos = end;
         let span = mk_sp(start, end);
         let snippet = self.snippet(span);
+
+        // Do nothing for spaces in the beginning of the file
+        if start == BytePos(0) && end.0 as usize == snippet.len() && snippet.trim().is_empty() {
+            return;
+        }
+
         if snippet.trim().is_empty() && !out_of_file_lines_range!(self, span) {
             // Keep vertical spaces within range.
             self.push_vertical_spaces(count_newlines(snippet));
@@ -122,7 +138,7 @@ fn push_vertical_spaces(&mut self, mut newline_count: usize) {
             }
         }
 
-        let blank_lines: String = repeat('\n').take(newline_count).collect();
+        let blank_lines = "\n".repeat(newline_count);
         self.push_str(&blank_lines);
     }
 
@@ -143,9 +159,9 @@ fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
         // Get a snippet from the file start to the span's hi without allocating.
         // We need it to determine what precedes the current comment. If the comment
         // follows code on the same line, we won't touch it.
-        let big_span_lo = self.codemap.lookup_char_pos(span.lo()).file.start_pos;
-        let local_begin = self.codemap.lookup_byte_offset(big_span_lo);
-        let local_end = self.codemap.lookup_byte_offset(span.hi());
+        let big_span_lo = self.source_map.lookup_char_pos(span.lo()).file.start_pos;
+        let local_begin = self.source_map.lookup_byte_offset(big_span_lo);
+        let local_end = self.source_map.lookup_byte_offset(span.hi());
         let start_index = local_begin.pos.to_usize();
         let end_index = local_end.pos.to_usize();
         let big_snippet = &local_begin.fm.src.as_ref().unwrap()[start_index..end_index];
@@ -171,12 +187,12 @@ fn write_snippet_inner<F>(
         // Trim whitespace from the right hand side of each line.
         // Annoyingly, the library functions for splitting by lines etc. are not
         // quite right, so we must do it ourselves.
-        let char_pos = self.codemap.lookup_char_pos(span.lo());
-        let file_name = &char_pos.file.name;
+        let char_pos = self.source_map.lookup_char_pos(span.lo());
+        let file_name = &char_pos.file.name.clone().into();
         let mut status = SnippetStatus::new(char_pos.line);
 
-        let snippet = &*match self.config.write_mode() {
-            WriteMode::Coverage => replace_chars(old_snippet),
+        let snippet = &*match self.config.emit_mode() {
+            EmitMode::Coverage => Cow::from(replace_chars(old_snippet)),
             _ => Cow::from(old_snippet),
         };
 
@@ -184,7 +200,7 @@ fn write_snippet_inner<F>(
             debug!("{:?}: {:?}", kind, subslice);
 
             let newline_count = count_newlines(subslice);
-            let within_file_lines_range = self.config.file_lines().intersects_range(
+            let within_file_lines_range = self.config.file_lines().contains_range(
                 file_name,
                 status.cur_line,
                 status.cur_line + newline_count,
@@ -286,7 +302,8 @@ fn process_missing_code(
             i += offset;
 
             if c == '\n' {
-                let skip_this_line = !self.config
+                let skip_this_line = !self
+                    .config
                     .file_lines()
                     .contains_line(file_name, status.cur_line);
                 if skip_this_line {
@@ -321,11 +338,9 @@ fn process_missing_code(
     }
 }
 
-fn replace_chars(string: &str) -> Cow<str> {
-    Cow::from(
-        string
-            .chars()
-            .map(|ch| if ch.is_whitespace() { ch } else { 'X' })
-            .collect::<String>(),
-    )
+fn replace_chars(string: &str) -> String {
+    string
+        .chars()
+        .map(|ch| if ch.is_whitespace() { ch } else { 'X' })
+        .collect()
 }