diff --git a/src/bin/main.rs b/src/bin/main.rs index 4c9798f6c72..2b5d5209b4f 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -19,7 +19,7 @@ use crate::rustfmt::{ load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, - FormatReportFormatterBuilder, Input, Session, Verbosity, + FormatReportFormatterBuilder, Input, Session, StyleEdition, Verbosity, }; const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rustfmt/issues/new?labels=bug"; @@ -129,7 +129,12 @@ 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|2021]"); + opts.optopt( + "", + "edition", + "Rust edition to use", + "[2015|2018|2021|2024]", + ); opts.optopt( "", "color", @@ -181,6 +186,12 @@ fn make_opts() -> Options { "skip-children", "Don't reformat child modules (unstable).", ); + opts.optopt( + "", + "style-edition", + "The edition of the Style Guide (unstable).", + "[2015|2018|2021|2024]", + ); } opts.optflag("v", "verbose", "Print verbose output"); @@ -525,6 +536,7 @@ struct GetOptsOptions { backup: bool, check: bool, edition: Option, + style_edition: Option, color: Option, file_lines: FileLines, // Default is all lines in all files. unstable_features: bool, @@ -556,6 +568,10 @@ pub fn from_matches(matches: &Matches) -> Result { if let Some(ref file_lines) = matches.opt_str("file-lines") { options.file_lines = file_lines.parse()?; } + if let Some(ref edition_str) = matches.opt_str("style-edition") { + options.style_edition = + Some(style_edition_from_style_edition_str(edition_str)?); + } } else { let mut unstable_options = vec![]; if matches.opt_present("skip-children") { @@ -567,6 +583,9 @@ pub fn from_matches(matches: &Matches) -> Result { if matches.opt_present("file-lines") { unstable_options.push("`--file-lines`"); } + if matches.opt_present("style-edition") { + unstable_options.push("`--style-edition`"); + } if !unstable_options.is_empty() { let s = if unstable_options.len() == 1 { "" } else { "s" }; return Err(format_err!( @@ -688,6 +707,9 @@ fn apply_to(self, config: &mut Config) { if let Some(edition) = self.edition { config.set_cli().edition(edition); } + if let Some(edition) = self.style_edition { + config.set().style_edition(edition); + } if self.check { config.set_cli().emit_mode(EmitMode::Diff); } else if let Some(emit_mode) = self.emit_mode { @@ -723,6 +745,16 @@ fn edition_from_edition_str(edition_str: &str) -> Result { } } +fn style_edition_from_style_edition_str(edition_str: &str) -> Result { + match edition_str { + "2015" => Ok(StyleEdition::Edition2015), + "2018" => Ok(StyleEdition::Edition2018), + "2021" => Ok(StyleEdition::Edition2021), + "2024" => Ok(StyleEdition::Edition2024), + _ => Err(format_err!("Invalid value for `--style-edition`")), + } +} + fn emit_mode_from_emit_str(emit_str: &str) -> Result { match emit_str { "files" => Ok(EmitMode::Files),