]> git.lizzy.rs Git - rust.git/blobdiff - src/formatting.rs
Merge pull request #3129 from otavio/issue-3104
[rust.git] / src / formatting.rs
index 3012837ba640ea83af3829f90e466467948464f2..039276ba6e886b39a54b1581d3ef459ce30833f7 100644 (file)
@@ -64,7 +64,7 @@ fn format_project<T: FormatHandler>(
     config: &Config,
     handler: &mut T,
 ) -> Result<FormatReport, ErrorKind> {
-    let mut timer = Timer::Initialized(Instant::now());
+    let mut timer = Timer::start();
 
     let main_file = input.file_name();
     let input_is_stdin = main_file == FileName::Stdin;
@@ -173,9 +173,11 @@ fn format_file(
         if visitor.macro_rewrite_failure {
             self.report.add_macro_format_failure();
         }
+        self.report
+            .add_non_formatted_ranges(visitor.skipped_range.clone());
 
         self.handler
-            .handle_formatted_file(path, visitor.buffer, &mut self.report)
+            .handle_formatted_file(path, visitor.buffer.to_owned(), &mut self.report)
     }
 }
 
@@ -194,11 +196,11 @@ impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
     fn handle_formatted_file(
         &mut self,
         path: FileName,
-        mut result: String,
+        result: String,
         report: &mut FormatReport,
     ) -> Result<(), ErrorKind> {
         if let Some(ref mut out) = self.out {
-            match source_file::write_file(&mut result, &path, out, &self.config) {
+            match source_file::write_file(&result, &path, out, &self.config) {
                 Ok(b) if b => report.add_diff(),
                 Err(e) => {
                     // Create a new error with path_str to help users see which files failed
@@ -224,7 +226,7 @@ pub(crate) struct FormattingError {
 
 impl FormattingError {
     pub(crate) fn from_span(
-        span: &Span,
+        span: Span,
         source_map: &SourceMap,
         kind: ErrorKind,
     ) -> FormattingError {
@@ -234,13 +236,14 @@ pub(crate) fn from_span(
             kind,
             is_string: false,
             line_buffer: source_map
-                .span_to_lines(*span)
+                .span_to_lines(span)
                 .ok()
                 .and_then(|fl| {
                     fl.file
                         .get_line(fl.lines[0].line_index)
                         .map(|l| l.into_owned())
-                }).unwrap_or_else(|| String::new()),
+                })
+                .unwrap_or_else(String::new),
         }
     }
 
@@ -271,6 +274,7 @@ pub(crate) fn format_len(&self) -> (usize, usize) {
             ErrorKind::LineOverflow(found, max) => (max, found - max),
             ErrorKind::TrailingWhitespace
             | ErrorKind::DeprecatedAttr
+            | ErrorKind::BadIssue(_)
             | ErrorKind::BadAttr
             | ErrorKind::LostComment => {
                 let trailing_ws_start = self
@@ -344,14 +348,23 @@ pub(crate) struct ModifiedLines {
 
 #[derive(Clone, Copy, Debug)]
 enum Timer {
+    Disabled,
     Initialized(Instant),
     DoneParsing(Instant, Instant),
     DoneFormatting(Instant, Instant, Instant),
 }
 
 impl Timer {
+    fn start() -> Timer {
+        if cfg!(target_arch = "wasm32") {
+            Timer::Disabled
+        } else {
+            Timer::Initialized(Instant::now())
+        }
+    }
     fn done_parsing(self) -> Self {
         match self {
+            Timer::Disabled => Timer::Disabled,
             Timer::Initialized(init_time) => Timer::DoneParsing(init_time, Instant::now()),
             _ => panic!("Timer can only transition to DoneParsing from Initialized state"),
         }
@@ -359,6 +372,7 @@ fn done_parsing(self) -> Self {
 
     fn done_formatting(self) -> Self {
         match self {
+            Timer::Disabled => Timer::Disabled,
             Timer::DoneParsing(init_time, parse_time) => {
                 Timer::DoneFormatting(init_time, parse_time, Instant::now())
             }
@@ -369,6 +383,7 @@ fn done_formatting(self) -> Self {
     /// Returns the time it took to parse the source files in seconds.
     fn get_parse_time(&self) -> f32 {
         match *self {
+            Timer::Disabled => panic!("this platform cannot time execution"),
             Timer::DoneParsing(init, parse_time) | Timer::DoneFormatting(init, parse_time, _) => {
                 // This should never underflow since `Instant::now()` guarantees monotonicity.
                 Self::duration_to_f32(parse_time.duration_since(init))
@@ -381,6 +396,7 @@ fn get_parse_time(&self) -> f32 {
     /// not included.
     fn get_format_time(&self) -> f32 {
         match *self {
+            Timer::Disabled => panic!("this platform cannot time execution"),
             Timer::DoneFormatting(_init, parse_time, format_time) => {
                 Self::duration_to_f32(format_time.duration_since(parse_time))
             }
@@ -514,7 +530,8 @@ fn new_line(&mut self, kind: FullCodeCharKind) {
                 && !self.is_skipped_line()
                 && self.should_report_error(kind, &error_kind)
             {
-                self.push_err(error_kind, kind.is_comment(), self.is_string);
+                let is_string = self.is_string;
+                self.push_err(error_kind, kind.is_comment(), is_string);
             }
         }