about summary refs log tree commit diff
path: root/compiler/rustc_driver/src
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2021-02-16 08:09:07 +0100
committerest31 <MTest31@outlook.com>2021-02-16 14:19:32 +0100
commite527def9c7a090fabf9ca41d09b629e5268d41f8 (patch)
tree953af5678f995cd957d80e44431977032e595094 /compiler/rustc_driver/src
parent42a4673fbd40b09a99d057eaa9b3e5579b54c184 (diff)
downloadrust-e527def9c7a090fabf9ca41d09b629e5268d41f8.tar.gz
rust-e527def9c7a090fabf9ca41d09b629e5268d41f8.zip
Replace File::create and write_all with fs::write
Also don't convert to u8 buffers and back
when we are only creating strings.
Diffstat (limited to 'compiler/rustc_driver/src')
-rw-r--r--compiler/rustc_driver/src/pretty.rs22
1 files changed, 11 insertions, 11 deletions
diff --git a/compiler/rustc_driver/src/pretty.rs b/compiler/rustc_driver/src/pretty.rs
index b7edc24bc4a..1729cf99862 100644
--- a/compiler/rustc_driver/src/pretty.rs
+++ b/compiler/rustc_driver/src/pretty.rs
@@ -15,8 +15,6 @@ use rustc_span::symbol::Ident;
 use rustc_span::FileName;
 
 use std::cell::Cell;
-use std::fs::File;
-use std::io::Write;
 use std::path::Path;
 
 pub use self::PpMode::*;
@@ -375,13 +373,14 @@ fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
     (src, src_name)
 }
 
-fn write_output(out: Vec<u8>, ofile: Option<&Path>) {
+fn write_or_print(out: &str, ofile: Option<&Path>) {
     match ofile {
-        None => print!("{}", String::from_utf8(out).unwrap()),
-        Some(p) => match File::create(p) {
-            Ok(mut w) => w.write_all(&out).unwrap(),
-            Err(e) => panic!("print-print failed to open {} due to {}", p.display(), e),
-        },
+        None => print!("{}", out),
+        Some(p) => {
+            if let Err(e) = std::fs::write(p, out) {
+                panic!("print-print failed to write {} due to {}", p.display(), e);
+            }
+        }
     }
 }
 
@@ -417,7 +416,7 @@ pub fn print_after_parsing(
         unreachable!();
     };
 
-    write_output(out.into_bytes(), ofile);
+    write_or_print(&out, ofile);
 }
 
 pub fn print_after_hir_lowering<'tcx>(
@@ -477,7 +476,7 @@ pub fn print_after_hir_lowering<'tcx>(
         _ => unreachable!(),
     }
 
-    write_output(out.into_bytes(), ofile);
+    write_or_print(&out, ofile);
 }
 
 // In an ideal world, this would be a public function called by the driver after
@@ -503,7 +502,8 @@ fn print_with_analysis(
     }
     .unwrap();
 
-    write_output(out, ofile);
+    let out = std::str::from_utf8(&out).unwrap();
+    write_or_print(out, ofile);
 
     Ok(())
 }