]> 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 a3d2819e1db7f7e6cd741227afeb99b6e9480ff7..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
@@ -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),