]> git.lizzy.rs Git - rust.git/blobdiff - src/bin/main.rs
Use edition in rustfmt.toml when no command line argument is passed
[rust.git] / src / bin / main.rs
index 760333955a054599991ed37c0a43dc0539bd1e57..b3c61b3df428fe3e6dc2177829bf87cd0b0dcb19 100644 (file)
@@ -8,9 +8,6 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#![cfg(not(test))]
-#![feature(extern_prelude)]
-
 extern crate env_logger;
 #[macro_use]
 extern crate failure;
@@ -28,8 +25,8 @@
 use getopts::{Matches, Options};
 
 use rustfmt::{
-    load_config, CliOptions, Color, Config, EmitMode, ErrorKind, FileLines, FileName, Input,
-    Session, Verbosity,
+    load_config, CliOptions, Color, Config, Edition, EmitMode, ErrorKind, FileLines, FileName,
+    Input, Session, Verbosity,
 };
 
 fn main() {
@@ -87,7 +84,7 @@ fn make_opts() -> Options {
     opts.optflag(
         "",
         "check",
-        "Run in 'check' mode. Exits with 0 if input if formatted correctly. Exits \
+        "Run in 'check' mode. Exits with 0 if input is formatted correctly. Exits \
          with 1 and prints a diff if formatting is required.",
     );
     let is_nightly = is_nightly();
@@ -105,6 +102,7 @@ fn make_opts() -> Options {
          found reverts to the input file path",
         "[Path for the configuration file]",
     );
+    opts.optopt("", "edition", "Rust edition to use", "[2015|2018]");
     opts.optopt(
         "",
         "color",
@@ -159,9 +157,7 @@ fn make_opts() -> Options {
 }
 
 fn is_nightly() -> bool {
-    option_env!("CFG_RELEASE_CHANNEL")
-        .map(|c| c == "nightly" || c == "dev")
-        .unwrap_or(false)
+    option_env!("CFG_RELEASE_CHANNEL").map_or(false, |c| c == "nightly" || c == "dev")
 }
 
 // Returned i32 is an exit code
@@ -172,19 +168,19 @@ fn execute(opts: &Options) -> Result<i32, failure::Error> {
     match determine_operation(&matches)? {
         Operation::Help(HelpOp::None) => {
             print_usage_to_stdout(opts, "");
-            return Ok(0);
+            Ok(0)
         }
         Operation::Help(HelpOp::Config) => {
             Config::print_docs(&mut stdout(), options.unstable_features);
-            return Ok(0);
+            Ok(0)
         }
         Operation::Help(HelpOp::FileLines) => {
             print_help_file_lines();
-            return Ok(0);
+            Ok(0)
         }
         Operation::Version => {
             print_version();
-            return Ok(0);
+            Ok(0)
         }
         Operation::ConfigOutputDefault { path } => {
             let toml = Config::default().all_options().to_toml().map_err(err_msg)?;
@@ -194,13 +190,13 @@ fn execute(opts: &Options) -> Result<i32, failure::Error> {
             } else {
                 io::stdout().write_all(toml.as_bytes())?;
             }
-            return Ok(0);
+            Ok(0)
         }
         Operation::Stdin { input } => format_string(input, options),
         Operation::Format {
             files,
             minimal_config_path,
-        } => format(files, minimal_config_path, options),
+        } => format(files, minimal_config_path, &options),
     }
 }
 
@@ -236,7 +232,7 @@ fn format_string(input: String, options: GetOptsOptions) -> Result<i32, failure:
 fn format(
     files: Vec<PathBuf>,
     minimal_config_path: Option<String>,
-    options: GetOptsOptions,
+    options: &GetOptsOptions,
 ) -> Result<i32, failure::Error> {
     options.verify_file_lines(&files);
     let (config, config_path) = load_config(None, Some(options.clone()))?;
@@ -421,7 +417,8 @@ fn determine_operation(matches: &Matches) -> Result<Operation, ErrorKind> {
             // we will do comparison later, so here tries to canonicalize first
             // to get the expected behavior.
             p.canonicalize().unwrap_or(p)
-        }).collect();
+        })
+        .collect();
 
     Ok(Operation::Format {
         files,
@@ -441,6 +438,7 @@ struct GetOptsOptions {
     emit_mode: EmitMode,
     backup: bool,
     check: bool,
+    edition: Option<Edition>,
     color: Option<Color>,
     file_lines: FileLines, // Default is all lines in all files.
     unstable_features: bool,
@@ -500,11 +498,12 @@ pub fn from_matches(matches: &Matches) -> Result<GetOptsOptions, failure::Error>
             if options.check {
                 return Err(format_err!("Invalid to use `--emit` and `--check`"));
             }
-            if let Ok(emit_mode) = emit_mode_from_emit_str(emit_str) {
-                options.emit_mode = emit_mode;
-            } else {
-                return Err(format_err!("Invalid value for `--emit`"));
-            }
+
+            options.emit_mode = emit_mode_from_emit_str(emit_str)?;
+        }
+
+        if let Some(ref edition_str) = matches.opt_str("edition") {
+            options.edition = Some(edition_from_edition_str(edition_str)?);
         }
 
         if matches.opt_present("backup") {
@@ -560,6 +559,9 @@ fn apply_to(self, config: &mut Config) {
         if let Some(error_on_unformatted) = self.error_on_unformatted {
             config.set().error_on_unformatted(error_on_unformatted);
         }
+        if let Some(edition) = self.edition {
+            config.set().edition(edition);
+        }
         if self.check {
             config.set().emit_mode(EmitMode::Diff);
         } else {
@@ -578,6 +580,14 @@ fn config_path(&self) -> Option<&Path> {
     }
 }
 
+fn edition_from_edition_str(edition_str: &str) -> Result<Edition, failure::Error> {
+    match edition_str {
+        "2015" => Ok(Edition::Edition2015),
+        "2018" => Ok(Edition::Edition2018),
+        _ => Err(format_err!("Invalid value for `--edition`")),
+    }
+}
+
 fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode, failure::Error> {
     match emit_str {
         "files" => Ok(EmitMode::Files),