diff --git a/src/bin/main.rs b/src/bin/main.rs index 1b1c1d412c83..03105a902208 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -26,7 +26,7 @@ use getopts::{Matches, Options}; use rustfmt::{ emit_post_matter, emit_pre_matter, format_and_emit_report, load_config, CliOptions, Config, - FileName, FmtResult, Input, Summary, Verbosity, WriteMode, WRITE_MODE_LIST, + FileName, FmtResult, Input, Summary, Verbosity, WriteMode, }; fn main() { @@ -111,7 +111,13 @@ fn make_opts() -> Options { found reverts to the input file path", "[Path for the configuration file]", ); - opts.optopt("", "emit", "What data to emit and how", WRITE_MODE_LIST); + let is_nightly = is_nightly(); + let emit_opts = if is_nightly { + "[files|stdout|coverage|checkstyle]" + } else { + "[files|stdout]" + }; + opts.optopt("", "emit", "What data to emit and how", emit_opts); opts.optflagopt( "h", "help", @@ -129,7 +135,7 @@ fn make_opts() -> Options { opts.optflag("q", "quiet", "Print less output"); opts.optflag("V", "version", "Show version information"); - if is_nightly() { + if is_nightly { opts.optflag( "", "unstable-features", diff --git a/src/config/options.rs b/src/config/options.rs index 15915bb99760..659152258605 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -14,7 +14,7 @@ use config::config_type::ConfigType; use config::file_lines::FileLines; use config::lists::*; use config::Config; -use {FmtResult, WRITE_MODE_LIST}; +use FmtResult; use failure::err_msg; @@ -174,14 +174,12 @@ configuration_option_enum! { ReportTactic: } configuration_option_enum! { WriteMode: - // Backs the original file up and overwrites the original. - Replace, // Overwrites original file without backup. Overwrite, + // Backs the original file up and overwrites the original. + Replace, // Writes the output to stdout. Display, - // Writes the diff to stdout. - Diff, // Displays how much of the input file was processed Coverage, // Unfancy stdout @@ -196,6 +194,13 @@ configuration_option_enum! { WriteMode: None, } +const STABLE_WRITE_MODES: [WriteMode; 4] = [ + WriteMode::Replace, + WriteMode::Overwrite, + WriteMode::Display, + WriteMode::Check, +]; + configuration_option_enum! { Color: // Always use color, whether it is a piped or terminal output Always, @@ -331,7 +336,7 @@ pub struct CliOptions { pub quiet: bool, pub verbose: bool, pub config_path: Option, - pub write_mode: Option, + pub write_mode: WriteMode, pub check: bool, pub color: Option, pub file_lines: FileLines, // Default is all lines in all files. @@ -355,7 +360,7 @@ impl CliOptions { options.unstable_features = matches.opt_present("unstable-features"); } - if options.unstable_features { + if !options.unstable_features { if matches.opt_present("skip-children") { options.skip_children = Some(true); } @@ -375,16 +380,21 @@ impl CliOptions { return Err(format_err!("Invalid to use `--emit` and `--check`")); } if let Ok(write_mode) = write_mode_from_emit_str(emit_str) { - if write_mode == WriteMode::Overwrite && matches.opt_present("backup") { - options.write_mode = Some(WriteMode::Replace); - } else { - options.write_mode = Some(write_mode); - } + options.write_mode = write_mode; } else { + return Err(format_err!("Invalid value for `--emit`")); + } + } + + if options.write_mode == WriteMode::Overwrite && matches.opt_present("backup") { + options.write_mode = WriteMode::Replace; + } + + if !rust_nightly { + if !STABLE_WRITE_MODES.contains(&options.write_mode) { return Err(format_err!( - "Invalid value for `--emit`: {}, expected one of {}", - emit_str, - WRITE_MODE_LIST + "Invalid value for `--emit` - using an unstable \ + value without `--unstable-features`", )); } } @@ -417,8 +427,8 @@ impl CliOptions { } if self.check { config.set().write_mode(WriteMode::Check); - } else if let Some(write_mode) = self.write_mode { - config.set().write_mode(write_mode); + } else { + config.set().write_mode(self.write_mode); } if let Some(color) = self.color { config.set().color(color); diff --git a/src/filemap.rs b/src/filemap.rs index fc86beeede46..d61a3aa2a74e 100644 --- a/src/filemap.rs +++ b/src/filemap.rs @@ -156,19 +156,6 @@ where } write_system_newlines(out, text, config)?; } - WriteMode::Diff => { - let filename = filename_to_path(); - if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) { - let mismatch = make_diff(&ori, &fmt, 3); - let has_diff = !mismatch.is_empty(); - print_diff( - mismatch, - |line_num| format!("Diff in {} at line {}:", filename.display(), line_num), - config, - ); - return Ok(has_diff); - } - } WriteMode::Modified => { let filename = filename_to_path(); if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) { diff --git a/src/lib.rs b/src/lib.rs index 5fb3e4b03e35..c50c45fbd36a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,9 +68,6 @@ pub use config::{file_lines, load_config, Config, Verbosity, WriteMode}; pub type FmtResult = std::result::Result; -// FIXME: this is badly named since the user-facing name is `emit` not `write-mode`. -pub const WRITE_MODE_LIST: &str = "[files|stdout|coverage|checkstyle]"; - #[macro_use] mod utils;