From 294b463b5d9755fe2138b1d7a8593aa263567bdf Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Sat, 2 Apr 2016 19:36:56 +0300
Subject: [PATCH 01/10] minor: use &mut ref instead of by value argument

This also unifies `write_all_files` and `write_file` functions
---
 src/filemap.rs | 8 ++++----
 src/lib.rs     | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/filemap.rs b/src/filemap.rs
index f41915b8cff..64c892a0929 100644
--- a/src/filemap.rs
+++ b/src/filemap.rs
@@ -31,14 +31,14 @@ pub fn append_newlines(file_map: &mut FileMap) {
     }
 }
 
-pub fn write_all_files<T>(file_map: &FileMap, mut out: T, config: &Config) -> Result<(), io::Error>
+pub fn write_all_files<T>(file_map: &FileMap, out: &mut T, config: &Config) -> Result<(), io::Error>
     where T: Write
 {
-    output_header(&mut out, config.write_mode).ok();
+    output_header(out, config.write_mode).ok();
     for filename in file_map.keys() {
-        try!(write_file(&file_map[filename], filename, &mut out, config));
+        try!(write_file(&file_map[filename], filename, out, config));
     }
-    output_footer(&mut out, config.write_mode).ok();
+    output_footer(out, config.write_mode).ok();
 
     Ok(())
 }
diff --git a/src/lib.rs b/src/lib.rs
index 3443db338cf..a559c23006b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -432,8 +432,8 @@ pub fn run(file: &Path, config: &Config) {
     let mut result = format(file, config);
 
     print!("{}", fmt_lines(&mut result, config));
-    let out = stdout();
-    let write_result = filemap::write_all_files(&result, out, config);
+    let mut out = stdout();
+    let write_result = filemap::write_all_files(&result, &mut out, config);
 
     if let Err(msg) = write_result {
         println!("Error writing files: {}", msg);

From 84fb2f402e92976c34c4a60cab5b918d377388fb Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Sat, 2 Apr 2016 21:56:37 +0300
Subject: [PATCH 02/10] refactor: unify run and run_from_stdin

---
 src/bin/rustfmt.rs |  6 +++---
 src/filemap.rs     | 10 +++++-----
 src/lib.rs         | 42 +++++++++++++++++++++++++-----------------
 3 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs
index 062c60e1065..e3ead2a28a1 100644
--- a/src/bin/rustfmt.rs
+++ b/src/bin/rustfmt.rs
@@ -17,7 +17,7 @@ extern crate toml;
 extern crate env_logger;
 extern crate getopts;
 
-use rustfmt::{run, run_from_stdin};
+use rustfmt::{run, Input};
 use rustfmt::config::{Config, WriteMode};
 
 use std::env;
@@ -197,7 +197,7 @@ fn execute() -> i32 {
             // write_mode is always Plain for Stdin.
             config.write_mode = WriteMode::Plain;
 
-            run_from_stdin(input, &config);
+            run(Input::Text(input), &config);
             0
         }
         Operation::Format { files, config_path } => {
@@ -233,7 +233,7 @@ fn execute() -> i32 {
                     print_usage(&opts, &e);
                     return 1;
                 }
-                run(&file, &config);
+                run(Input::File(file), &config);
             }
             0
         }
diff --git a/src/filemap.rs b/src/filemap.rs
index 64c892a0929..c6af9a70184 100644
--- a/src/filemap.rs
+++ b/src/filemap.rs
@@ -80,11 +80,11 @@ pub fn write_system_newlines<T>(writer: T,
     }
 }
 
-pub fn write_file<T>(text: &StringBuffer,
-                     filename: &str,
-                     out: &mut T,
-                     config: &Config)
-                     -> Result<Option<String>, io::Error>
+fn write_file<T>(text: &StringBuffer,
+                 filename: &str,
+                 out: &mut T,
+                 config: &Config)
+                 -> Result<Option<String>, io::Error>
     where T: Write
 {
 
diff --git a/src/lib.rs b/src/lib.rs
index a559c23006b..42c5c7a5895 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -33,7 +33,7 @@ use syntax::parse::{self, ParseSess};
 
 use std::io::stdout;
 use std::ops::{Add, Sub};
-use std::path::Path;
+use std::path::{Path, PathBuf};
 use std::rc::Rc;
 use std::collections::HashMap;
 use std::fmt;
@@ -41,7 +41,7 @@ use std::fmt;
 use issues::{BadIssueSeeker, Issue};
 use filemap::FileMap;
 use visitor::FmtVisitor;
-use config::Config;
+use config::{Config, WriteMode};
 
 #[macro_use]
 mod utils;
@@ -428,27 +428,35 @@ pub fn format(file: &Path, config: &Config) -> FileMap {
     file_map
 }
 
-pub fn run(file: &Path, config: &Config) {
-    let mut result = format(file, config);
+fn format_input(input: Input, config: &Config) -> (FileMap, FormatReport) {
+    let mut file_map = match input {
+        Input::File(ref file) => format(file, config),
+        Input::Text(text) => format_string(text, config),
+    };
 
-    print!("{}", fmt_lines(&mut result, config));
-    let mut out = stdout();
-    let write_result = filemap::write_all_files(&result, &mut out, config);
-
-    if let Err(msg) = write_result {
-        println!("Error writing files: {}", msg);
-    }
+    let report = fmt_lines(&mut file_map, config);
+    (file_map, report)
 }
 
-// Similar to run, but takes an input String instead of a file to format
-pub fn run_from_stdin(input: String, config: &Config) {
-    let mut result = format_string(input, config);
-    fmt_lines(&mut result, config);
+pub enum Input {
+    File(PathBuf),
+    Text(String),
+}
+
+pub fn run(input: Input, config: &Config) {
+    let (file_map, report) = format_input(input, config);
+
+    let ignore_errors = config.write_mode == WriteMode::Plain;
+    if !ignore_errors {
+        print!("{}", report);
+    }
 
     let mut out = stdout();
-    let write_result = filemap::write_file(&result["stdin"], "stdin", &mut out, config);
+    let write_result = filemap::write_all_files(&file_map, &mut out, config);
 
     if let Err(msg) = write_result {
-        panic!("Error writing to stdout: {}", msg);
+        if !ignore_errors {
+            println!("Error writing files: {}", msg);
+        }
     }
 }

From bee9682a494d9e59cf8fdb48c5b642d70f8b949f Mon Sep 17 00:00:00 2001
From: Srinivas Reddy Thatiparthy <thatiparthysreenivas@gmail.com>
Date: Sun, 3 Apr 2016 01:08:25 +0530
Subject: [PATCH 03/10] Correct spelling of 'style'

---
 src/config.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/config.rs b/src/config.rs
index 32eb9af4b11..0b4b16c328e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -45,7 +45,7 @@ configuration_option_enum! { ReturnIndent:
     WithWhereClause,
 }
 
-// How to stle a struct literal.
+// How to style a struct literal.
 configuration_option_enum! { StructLitStyle:
     // First line on the same line as the opening brace, all lines aligned with
     // the first line.

From 255231d9edf467a6dbcf30f1ce943e750232012e Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Sat, 2 Apr 2016 23:41:25 +0300
Subject: [PATCH 04/10] don't read config twice during tests

---
 tests/system.rs | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/tests/system.rs b/tests/system.rs
index eac45b83ca0..7081d73f778 100644
--- a/tests/system.rs
+++ b/tests/system.rs
@@ -74,12 +74,8 @@ fn checkstyle_test() {
 // Helper function for comparing the results of rustfmt
 // to a known output file generated by one of the write modes.
 fn assert_output(source: &str, expected_filename: &str, write_mode: Option<WriteMode>) {
-    let file_map = run_rustfmt(source.to_string(), write_mode);
-
-    let mut config = read_config(&source);
-    if let Some(write_mode) = write_mode {
-        config.write_mode = write_mode;
-    }
+    let config = read_config(&source, write_mode);
+    let file_map = run_rustfmt(source.to_string(), &config);
 
     // Populate output by writing to a vec.
     let mut out = vec![];
@@ -180,7 +176,7 @@ fn print_mismatches(result: HashMap<String, Vec<Mismatch>>) {
     assert!(t.reset().unwrap());
 }
 
-fn read_config(filename: &str) -> Config {
+fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config {
     let sig_comments = read_significant_comments(&filename);
     let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..]));
 
@@ -192,15 +188,16 @@ fn read_config(filename: &str) -> Config {
 
     // Don't generate warnings for to-do items.
     config.report_todo = ReportTactic::Never;
+
+    if let Some(mode) = write_mode {
+        config.write_mode = mode
+    }
+
     config
 }
 
 // Simulate run()
-fn run_rustfmt(filename: String, write_mode: Option<WriteMode>) -> FileMap {
-    let mut config = read_config(&filename);
-    if let Some(write_mode) = write_mode {
-        config.write_mode = write_mode;
-    }
+fn run_rustfmt(filename: String, config: &Config) -> FileMap {
     format(Path::new(&filename), &config)
 }
 
@@ -208,8 +205,8 @@ pub fn idempotent_check(filename: String,
                         write_mode: Option<WriteMode>)
                         -> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
     let sig_comments = read_significant_comments(&filename);
-    let config = read_config(&filename);
-    let mut file_map = run_rustfmt(filename, write_mode);
+    let config = read_config(&filename, write_mode);
+    let mut file_map = run_rustfmt(filename, &config);
     let format_report = fmt_lines(&mut file_map, &config);
 
     let mut write_result = HashMap::new();

From ac4532e16117eff1ecd24597d2dd916196e4efd9 Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Sun, 3 Apr 2016 00:32:24 +0300
Subject: [PATCH 05/10] use format_input function in tests

---
 src/lib.rs                     |  4 ++--
 tests/system.rs                | 13 ++++++-------
 tests/writemode/checkstyle.xml |  2 +-
 3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index 42c5c7a5895..396ec030f7c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -403,7 +403,7 @@ pub fn format_string(input: String, config: &Config) -> FileMap {
     file_map
 }
 
-pub fn format(file: &Path, config: &Config) -> FileMap {
+fn format(file: &Path, config: &Config) -> FileMap {
     let codemap = Rc::new(CodeMap::new());
 
     let tty_handler = Handler::with_tty_emitter(ColorConfig::Auto,
@@ -428,7 +428,7 @@ pub fn format(file: &Path, config: &Config) -> FileMap {
     file_map
 }
 
-fn format_input(input: Input, config: &Config) -> (FileMap, FormatReport) {
+pub fn format_input(input: Input, config: &Config) -> (FileMap, FormatReport) {
     let mut file_map = match input {
         Input::File(ref file) => format(file, config),
         Input::Text(text) => format_string(text, config),
diff --git a/tests/system.rs b/tests/system.rs
index 7081d73f778..96a9d9b7f92 100644
--- a/tests/system.rs
+++ b/tests/system.rs
@@ -16,7 +16,7 @@ extern crate term;
 use std::collections::HashMap;
 use std::fs;
 use std::io::{self, Read, BufRead, BufReader};
-use std::path::Path;
+use std::path::{Path, PathBuf};
 
 use rustfmt::*;
 use rustfmt::filemap::{write_system_newlines, FileMap};
@@ -75,7 +75,7 @@ fn checkstyle_test() {
 // to a known output file generated by one of the write modes.
 fn assert_output(source: &str, expected_filename: &str, write_mode: Option<WriteMode>) {
     let config = read_config(&source, write_mode);
-    let file_map = run_rustfmt(source.to_string(), &config);
+    let (file_map, _report) = format_file(source, &config);
 
     // Populate output by writing to a vec.
     let mut out = vec![];
@@ -196,9 +196,9 @@ fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config {
     config
 }
 
-// Simulate run()
-fn run_rustfmt(filename: String, config: &Config) -> FileMap {
-    format(Path::new(&filename), &config)
+fn format_file<P: Into<PathBuf>>(filename: P, config: &Config) -> (FileMap, FormatReport) {
+    let input = Input::File(filename.into());
+    format_input(input, &config)
 }
 
 pub fn idempotent_check(filename: String,
@@ -206,8 +206,7 @@ pub fn idempotent_check(filename: String,
                         -> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
     let sig_comments = read_significant_comments(&filename);
     let config = read_config(&filename, write_mode);
-    let mut file_map = run_rustfmt(filename, &config);
-    let format_report = fmt_lines(&mut file_map, &config);
+    let (file_map, format_report) = format_file(filename, &config);
 
     let mut write_result = HashMap::new();
     for (filename, text) in file_map.iter() {
diff --git a/tests/writemode/checkstyle.xml b/tests/writemode/checkstyle.xml
index f655cfb3b6b..12c7dd9fdf1 100644
--- a/tests/writemode/checkstyle.xml
+++ b/tests/writemode/checkstyle.xml
@@ -1,2 +1,2 @@
 <?xml version="1.0" encoding="utf-8"?>
-<checkstyle version="4.3"><file name="tests/source/fn-single-line.rs"><error line="1" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="1" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="1" severity="warning" message="Should be `fn empty() {}`" /><error line="1" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="1" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="1" severity="warning" message="Should be `    where T: Sync`" /><error line="1" severity="warning" message="Should be `{`" /><error line="50" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="57" severity="warning" message="Should be `    fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="57" severity="warning" message="Should be `    fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /><error line="57" severity="warning" message="Should be ``" /></file></checkstyle>
+<checkstyle version="4.3"><file name="tests/source/fn-single-line.rs"><error line="1" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="1" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="1" severity="warning" message="Should be `fn empty() {}`" /><error line="1" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="1" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="1" severity="warning" message="Should be `    where T: Sync`" /><error line="1" severity="warning" message="Should be `{`" /><error line="50" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="57" severity="warning" message="Should be `    fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="57" severity="warning" message="Should be `    fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /></file></checkstyle>

From 63887ed4132dc362e4d5a271672f352c74154613 Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Sun, 3 Apr 2016 00:45:04 +0300
Subject: [PATCH 06/10] make format_string and fmt_lines private

---
 src/lib.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index 396ec030f7c..5121a034fa4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -287,7 +287,7 @@ fn fmt_ast(krate: &ast::Crate,
 // Formatting done on a char by char or line by line basis.
 // TODO(#209) warn on bad license
 // TODO(#20) other stuff for parity with make tidy
-pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
+fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
     let mut truncate_todo = Vec::new();
     let mut report = FormatReport { file_error_map: HashMap::new() };
 
@@ -367,7 +367,7 @@ pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
     report
 }
 
-pub fn format_string(input: String, config: &Config) -> FileMap {
+fn format_string(input: String, config: &Config) -> FileMap {
     let path = "stdin";
     let codemap = Rc::new(CodeMap::new());
 

From 6e393b3d53aba6f3d18b76fd820c148f9e4b62e7 Mon Sep 17 00:00:00 2001
From: 0x0G <0x0G@users.noreply.github.com>
Date: Mon, 4 Apr 2016 13:49:16 +0300
Subject: [PATCH 07/10] Fix. rustfmt write to stderr instead stdout

Fix. rustfmt write to stderr instead stdout
---
 src/bin/rustfmt.rs | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs
index 062c60e1065..666185f837e 100644
--- a/src/bin/rustfmt.rs
+++ b/src/bin/rustfmt.rs
@@ -212,7 +212,7 @@ fn execute() -> i32 {
                 path = path_tmp;
             };
             if let Some(path) = path.as_ref() {
-                msg!("Using rustfmt config file {}", path.display());
+                println!("Using rustfmt config file {}", path.display());
             }
             for file in files {
                 // Check the file directory if the config-path could not be read or not provided
@@ -222,9 +222,9 @@ fn execute() -> i32 {
                                                                        for {}",
                                                                       file.display()));
                     if let Some(path) = path_tmp.as_ref() {
-                        msg!("Using rustfmt config file {} for {}",
-                             path.display(),
-                             file.display());
+                        println!("Using rustfmt config file {} for {}",
+                                 path.display(),
+                                 file.display());
                     }
                     config = config_tmp;
                 }

From c29ee66b94fea672f459374942a4e311dc9cef5e Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Tue, 5 Apr 2016 01:20:14 +0300
Subject: [PATCH 08/10] make naming more consistent

---
 src/lib.rs | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index 5121a034fa4..8c69710e359 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -287,7 +287,7 @@ fn fmt_ast(krate: &ast::Crate,
 // Formatting done on a char by char or line by line basis.
 // TODO(#209) warn on bad license
 // TODO(#20) other stuff for parity with make tidy
-fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
+fn format_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
     let mut truncate_todo = Vec::new();
     let mut report = FormatReport { file_error_map: HashMap::new() };
 
@@ -403,7 +403,7 @@ fn format_string(input: String, config: &Config) -> FileMap {
     file_map
 }
 
-fn format(file: &Path, config: &Config) -> FileMap {
+fn format_file(file: &Path, config: &Config) -> FileMap {
     let codemap = Rc::new(CodeMap::new());
 
     let tty_handler = Handler::with_tty_emitter(ColorConfig::Auto,
@@ -430,11 +430,11 @@ fn format(file: &Path, config: &Config) -> FileMap {
 
 pub fn format_input(input: Input, config: &Config) -> (FileMap, FormatReport) {
     let mut file_map = match input {
-        Input::File(ref file) => format(file, config),
+        Input::File(ref file) => format_file(file, config),
         Input::Text(text) => format_string(text, config),
     };
 
-    let report = fmt_lines(&mut file_map, config);
+    let report = format_lines(&mut file_map, config);
     (file_map, report)
 }
 

From 07cf62fc427635fef235dfad0272a0d936f31380 Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
Date: Tue, 5 Apr 2016 02:02:22 +0300
Subject: [PATCH 09/10] simplify tests

* change layout so that test group `foo` has source files in
`test/foo/source` and target files in `test/foo/target`.

* use significant comments to specify write mode instead of threading
  Option<WriteMode>
---
 .../source}/comments.rs                       |  1 +
 .../target}/comments.rs                       |  1 +
 tests/system.rs                               | 78 +++++++-----------
 tests/writemode/checkstyle.xml                |  2 -
 tests/writemode/source/fn-single-line.rs      | 80 +++++++++++++++++++
 tests/writemode/target/checkstyle.xml         |  2 +
 6 files changed, 115 insertions(+), 49 deletions(-)
 rename tests/{coverage-source => coverage/source}/comments.rs (84%)
 rename tests/{coverage-target => coverage/target}/comments.rs (84%)
 delete mode 100644 tests/writemode/checkstyle.xml
 create mode 100644 tests/writemode/source/fn-single-line.rs
 create mode 100644 tests/writemode/target/checkstyle.xml

diff --git a/tests/coverage-source/comments.rs b/tests/coverage/source/comments.rs
similarity index 84%
rename from tests/coverage-source/comments.rs
rename to tests/coverage/source/comments.rs
index 379e8e5820e..e79557af713 100644
--- a/tests/coverage-source/comments.rs
+++ b/tests/coverage/source/comments.rs
@@ -1,3 +1,4 @@
+// rustfmt-write_mode: coverage
 /// Here's a doc comment!
 fn main() {
     // foo is bar
diff --git a/tests/coverage-target/comments.rs b/tests/coverage/target/comments.rs
similarity index 84%
rename from tests/coverage-target/comments.rs
rename to tests/coverage/target/comments.rs
index 74d17bffd15..8f9c223aef2 100644
--- a/tests/coverage-target/comments.rs
+++ b/tests/coverage/target/comments.rs
@@ -1,3 +1,4 @@
+XX XXXXXXXXXXXXXXXXXXX XXXXXXXX
 /// Here's a doc comment!
 fn main() {
     XX XXX XX XXX
diff --git a/tests/system.rs b/tests/system.rs
index 96a9d9b7f92..ef3486b0bdc 100644
--- a/tests/system.rs
+++ b/tests/system.rs
@@ -20,7 +20,7 @@ use std::path::{Path, PathBuf};
 
 use rustfmt::*;
 use rustfmt::filemap::{write_system_newlines, FileMap};
-use rustfmt::config::{Config, ReportTactic, WriteMode};
+use rustfmt::config::{Config, ReportTactic};
 use rustfmt::rustfmt_diff::*;
 
 const DIFF_CONTEXT_SIZE: usize = 3;
@@ -44,7 +44,7 @@ fn system_tests() {
     // Turn a DirEntry into a String that represents the relative path to the
     // file.
     let files = files.map(get_path_string);
-    let (_reports, count, fails) = check_files(files, None);
+    let (_reports, count, fails) = check_files(files);
 
     // Display results.
     println!("Ran {} system tests.", count);
@@ -55,9 +55,9 @@ fn system_tests() {
 // the only difference is the coverage mode
 #[test]
 fn coverage_tests() {
-    let files = fs::read_dir("tests/coverage-source").expect("Couldn't read source dir");
+    let files = fs::read_dir("tests/coverage/source").expect("Couldn't read source dir");
     let files = files.map(get_path_string);
-    let (_reports, count, fails) = check_files(files, Some(WriteMode::Coverage));
+    let (_reports, count, fails) = check_files(files);
 
     println!("Ran {} tests in coverage mode.", count);
     assert!(fails == 0, "{} tests failed", fails);
@@ -65,16 +65,16 @@ fn coverage_tests() {
 
 #[test]
 fn checkstyle_test() {
-    let filename = "tests/source/fn-single-line.rs";
-    let expected_filename = "tests/writemode/checkstyle.xml";
-    assert_output(filename, expected_filename, Some(WriteMode::Checkstyle));
+    let filename = "tests/writemode/source/fn-single-line.rs";
+    let expected_filename = "tests/writemode/target/checkstyle.xml";
+    assert_output(filename, expected_filename);
 }
 
 
 // Helper function for comparing the results of rustfmt
 // to a known output file generated by one of the write modes.
-fn assert_output(source: &str, expected_filename: &str, write_mode: Option<WriteMode>) {
-    let config = read_config(&source, write_mode);
+fn assert_output(source: &str, expected_filename: &str) {
+    let config = read_config(&source);
     let (file_map, _report) = format_file(source, &config);
 
     // Populate output by writing to a vec.
@@ -104,7 +104,7 @@ fn idempotence_tests() {
     let files = fs::read_dir("tests/target")
                     .expect("Couldn't read target dir")
                     .map(get_path_string);
-    let (_reports, count, fails) = check_files(files, None);
+    let (_reports, count, fails) = check_files(files);
 
     // Display results.
     println!("Ran {} idempotent tests.", count);
@@ -122,7 +122,7 @@ fn self_tests() {
     // Hack because there's no `IntoIterator` impl for `[T; N]`.
     let files = files.chain(Some("src/lib.rs".to_owned()).into_iter());
 
-    let (reports, count, fails) = check_files(files, None);
+    let (reports, count, fails) = check_files(files);
     let mut warnings = 0;
 
     // Display results.
@@ -141,7 +141,7 @@ fn self_tests() {
 
 // For each file, run rustfmt and collect the output.
 // Returns the number of files checked and the number of failures.
-fn check_files<I>(files: I, write_mode: Option<WriteMode>) -> (Vec<FormatReport>, u32, u32)
+fn check_files<I>(files: I) -> (Vec<FormatReport>, u32, u32)
     where I: Iterator<Item = String>
 {
     let mut count = 0;
@@ -151,7 +151,7 @@ fn check_files<I>(files: I, write_mode: Option<WriteMode>) -> (Vec<FormatReport>
     for file_name in files.filter(|f| f.ends_with(".rs")) {
         println!("Testing '{}'...", file_name);
 
-        match idempotent_check(file_name, write_mode) {
+        match idempotent_check(file_name) {
             Ok(report) => reports.push(report),
             Err(msg) => {
                 print_mismatches(msg);
@@ -176,7 +176,7 @@ fn print_mismatches(result: HashMap<String, Vec<Mismatch>>) {
     assert!(t.reset().unwrap());
 }
 
-fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config {
+fn read_config(filename: &str) -> Config {
     let sig_comments = read_significant_comments(&filename);
     let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..]));
 
@@ -189,10 +189,6 @@ fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config {
     // Don't generate warnings for to-do items.
     config.report_todo = ReportTactic::Never;
 
-    if let Some(mode) = write_mode {
-        config.write_mode = mode
-    }
-
     config
 }
 
@@ -201,11 +197,9 @@ fn format_file<P: Into<PathBuf>>(filename: P, config: &Config) -> (FileMap, Form
     format_input(input, &config)
 }
 
-pub fn idempotent_check(filename: String,
-                        write_mode: Option<WriteMode>)
-                        -> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
+pub fn idempotent_check(filename: String) -> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
     let sig_comments = read_significant_comments(&filename);
-    let config = read_config(&filename, write_mode);
+    let config = read_config(&filename);
     let (file_map, format_report) = format_file(filename, &config);
 
     let mut write_result = HashMap::new();
@@ -220,7 +214,7 @@ pub fn idempotent_check(filename: String,
 
     let target = sig_comments.get("target").map(|x| &(*x)[..]);
 
-    handle_result(write_result, target, write_mode).map(|_| format_report)
+    handle_result(write_result, target).map(|_| format_report)
 }
 
 // Reads test config file from comments and reads its contents.
@@ -268,14 +262,13 @@ fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
 // Compare output to input.
 // TODO: needs a better name, more explanation.
 fn handle_result(result: HashMap<String, String>,
-                 target: Option<&str>,
-                 write_mode: Option<WriteMode>)
+                 target: Option<&str>)
                  -> Result<(), HashMap<String, Vec<Mismatch>>> {
     let mut failures = HashMap::new();
 
     for (file_name, fmt_text) in result {
         // If file is in tests/source, compare to file with same name in tests/target.
-        let target = get_target(&file_name, target, write_mode);
+        let target = get_target(&file_name, target);
         let mut f = fs::File::open(&target).expect("Couldn't open target");
 
         let mut text = String::new();
@@ -297,29 +290,20 @@ fn handle_result(result: HashMap<String, String>,
 }
 
 // Map source file paths to their target paths.
-fn get_target(file_name: &str, target: Option<&str>, write_mode: Option<WriteMode>) -> String {
-    let file_path = Path::new(file_name);
-    let (source_path_prefix, target_path_prefix) = match write_mode {
-        Some(WriteMode::Coverage) => {
-            (Path::new("tests/coverage-source/"), "tests/coverage-target/")
+fn get_target(file_name: &str, target: Option<&str>) -> String {
+    if file_name.contains("source") {
+        let target_file_name = file_name.replace("source", "target");
+        if let Some(replace_name) = target {
+            Path::new(&target_file_name)
+                .with_file_name(replace_name)
+                .into_os_string()
+                .into_string()
+                .unwrap()
+        } else {
+            target_file_name
         }
-        _ => (Path::new("tests/source/"), "tests/target/"),
-    };
-
-    if file_path.starts_with(source_path_prefix) {
-        let mut components = file_path.components();
-        // Can't skip(2) as the resulting iterator can't as_path()
-        components.next();
-        components.next();
-
-        let new_target = match components.as_path().to_str() {
-            Some(string) => string,
-            None => file_name,
-        };
-        let base = target.unwrap_or(new_target);
-
-        format!("{}{}", target_path_prefix, base)
     } else {
+        // This is either and idempotence check or a self check
         file_name.to_owned()
     }
 }
diff --git a/tests/writemode/checkstyle.xml b/tests/writemode/checkstyle.xml
deleted file mode 100644
index 12c7dd9fdf1..00000000000
--- a/tests/writemode/checkstyle.xml
+++ /dev/null
@@ -1,2 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<checkstyle version="4.3"><file name="tests/source/fn-single-line.rs"><error line="1" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="1" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="1" severity="warning" message="Should be `fn empty() {}`" /><error line="1" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="1" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="1" severity="warning" message="Should be `    where T: Sync`" /><error line="1" severity="warning" message="Should be `{`" /><error line="50" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="57" severity="warning" message="Should be `    fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="57" severity="warning" message="Should be `    fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /></file></checkstyle>
diff --git a/tests/writemode/source/fn-single-line.rs b/tests/writemode/source/fn-single-line.rs
new file mode 100644
index 00000000000..289dd9aa093
--- /dev/null
+++ b/tests/writemode/source/fn-single-line.rs
@@ -0,0 +1,80 @@
+// rustfmt-fn_single_line: true
+// rustfmt-write_mode: checkstyle
+// Test single-line functions.
+
+fn foo_expr() {
+    1
+}
+
+fn foo_stmt() {
+    foo();
+}
+
+fn foo_decl_local()  {
+    let z = 5;
+   }
+
+fn    foo_decl_item(x: &mut i32) {
+    x = 3;
+}
+
+   fn empty()     {
+
+}
+
+fn foo_return() -> String {
+    "yay"
+}
+
+fn foo_where() -> T where T: Sync {
+    let x = 2;
+}
+
+fn fooblock() {
+    {
+        "inner-block"
+    }
+}
+
+fn fooblock2(x: i32) {
+    let z = match x {
+        _ => 2,
+    };
+}
+
+fn comment() {
+    // this is a test comment
+    1
+}
+
+fn comment2() {
+    // multi-line comment
+    let z = 2;
+    1
+}
+
+fn only_comment() {
+    // Keep this here
+}
+
+fn aaaaaaaaaaaaaaaaa_looooooooooooooooooooooong_name() {
+    let z = "aaaaaaawwwwwwwwwwwwwwwwwwwwwwwwwwww";
+}
+
+fn lots_of_space                      ()                                                           {
+                           1                 
+}
+
+fn mac() -> Vec<i32> { vec![] }
+
+trait CoolTypes {
+    fn dummy(&self) {
+    }
+}
+
+trait CoolerTypes { fn dummy(&self) { 
+}
+}
+
+fn Foo<T>() where T: Bar {
+}
diff --git a/tests/writemode/target/checkstyle.xml b/tests/writemode/target/checkstyle.xml
new file mode 100644
index 00000000000..b59d81b29c8
--- /dev/null
+++ b/tests/writemode/target/checkstyle.xml
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="utf-8"?>
+<checkstyle version="4.3"><file name="tests/writemode/source/fn-single-line.rs"><error line="2" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="2" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="2" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="2" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="2" severity="warning" message="Should be `fn empty() {}`" /><error line="2" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="2" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="2" severity="warning" message="Should be `    where T: Sync`" /><error line="2" severity="warning" message="Should be `{`" /><error line="51" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="58" severity="warning" message="Should be `    fn dummy(&amp;self) {}`" /><error line="58" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="58" severity="warning" message="Should be `    fn dummy(&amp;self) {}`" /><error line="58" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /></file></checkstyle>

From 901c5b1a5d45be3b9fe449716f779e31520e5882 Mon Sep 17 00:00:00 2001
From: Srinivas Reddy Thatiparthy <thatiparthysreenivas@gmail.com>
Date: Wed, 6 Apr 2016 10:04:29 +0530
Subject: [PATCH 10/10] use std::error instead std::out

---
 src/bin/rustfmt.rs | 10 +---------
 src/config.rs      |  5 +++--
 src/lib.rs         |  4 ++--
 src/utils.rs       | 10 ++++++++++
 4 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs
index bce8ac7bc0f..b65cbaa7f59 100644
--- a/src/bin/rustfmt.rs
+++ b/src/bin/rustfmt.rs
@@ -10,7 +10,7 @@
 
 #![cfg(not(test))]
 
-#[macro_use]
+
 extern crate log;
 extern crate rustfmt;
 extern crate toml;
@@ -28,14 +28,6 @@ use std::str::FromStr;
 
 use getopts::{Matches, Options};
 
-macro_rules! msg {
-    ($($arg:tt)*) => (
-        match writeln!(&mut ::std::io::stderr(), $($arg)* ) {
-            Ok(_) => {},
-            Err(x) => panic!("Unable to write to stderr: {}", x),
-        }
-    )
-}
 
 /// Rustfmt operations.
 enum Operation {
diff --git a/src/config.rs b/src/config.rs
index 0b4b16c328e..46c28322c92 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -11,6 +11,7 @@
 extern crate toml;
 
 use lists::{SeparatorTactic, ListTactic};
+use std::io::Write;
 
 macro_rules! configuration_option_enum{
     ($e:ident: $( $x:ident ),+ $(,)*) => {
@@ -222,9 +223,9 @@ macro_rules! create_config {
                 let parsed_config:ParsedConfig = match toml::decode(parsed) {
                     Some(decoded) => decoded,
                     None => {
-                        println!("Decoding config file failed. Config:\n{}", toml);
+                        msg!("Decoding config file failed. Config:\n{}", toml);
                         let parsed: toml::Value = toml.parse().expect("Could not parse TOML");
-                        println!("\n\nParsed:\n{:?}", parsed);
+                        msg!("\n\nParsed:\n{:?}", parsed);
                         panic!();
                     }
                 };
diff --git a/src/lib.rs b/src/lib.rs
index 8c69710e359..b7af96ac5aa 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -31,7 +31,7 @@ use syntax::errors::Handler;
 use syntax::errors::emitter::{ColorConfig, EmitterWriter};
 use syntax::parse::{self, ParseSess};
 
-use std::io::stdout;
+use std::io::{stdout, Write};
 use std::ops::{Add, Sub};
 use std::path::{Path, PathBuf};
 use std::rc::Rc;
@@ -456,7 +456,7 @@ pub fn run(input: Input, config: &Config) {
 
     if let Err(msg) = write_result {
         if !ignore_errors {
-            println!("Error writing files: {}", msg);
+            msg!("Error writing files: {}", msg);
         }
     }
 }
diff --git a/src/utils.rs b/src/utils.rs
index 902969b9618..66b9880777f 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -232,6 +232,16 @@ macro_rules! try_opt {
     })
 }
 
+macro_rules! msg {
+    ($($arg:tt)*) => (
+        match writeln!(&mut ::std::io::stderr(), $($arg)* ) {
+            Ok(_) => {},
+            Err(x) => panic!("Unable to write to stderr: {}", x),
+        }
+    )
+}
+
+
 // Wraps string-like values in an Option. Returns Some when the string adheres
 // to the Rewrite constraints defined for the Rewrite trait and else otherwise.
 pub fn wrap_str<S: AsRef<str>>(s: S, max_width: usize, width: usize, offset: Indent) -> Option<S> {