Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e41fcb1

Browse files
committed
rustfmt: add support to specify the Rust edition as argument
The new `--edition` command line argument allow the setting of the desired Rust edition to be used. Refs: rust-lang#3104. Signed-off-by: Otavio Salvador <[email protected]>
1 parent 2eab971 commit e41fcb1

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/bin/main.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use failure::err_msg;
2525
use getopts::{Matches, Options};
2626

2727
use rustfmt::{
28-
load_config, CliOptions, Color, Config, EmitMode, ErrorKind, FileLines, FileName, Input,
29-
Session, Verbosity,
28+
load_config, CliOptions, Color, Config, Edition, EmitMode, ErrorKind, FileLines, FileName,
29+
Input, Session, Verbosity,
3030
};
3131

3232
fn main() {
@@ -102,6 +102,7 @@ fn make_opts() -> Options {
102102
found reverts to the input file path",
103103
"[Path for the configuration file]",
104104
);
105+
opts.optopt("", "edition", "Rust edition to use", "[2015|2018]");
105106
opts.optopt(
106107
"",
107108
"color",
@@ -437,6 +438,7 @@ struct GetOptsOptions {
437438
emit_mode: EmitMode,
438439
backup: bool,
439440
check: bool,
441+
edition: Edition,
440442
color: Option<Color>,
441443
file_lines: FileLines, // Default is all lines in all files.
442444
unstable_features: bool,
@@ -500,6 +502,10 @@ impl GetOptsOptions {
500502
options.emit_mode = emit_mode_from_emit_str(emit_str)?;
501503
}
502504

505+
if let Some(ref edition_str) = matches.opt_str("edition") {
506+
options.edition = edition_from_edition_str(edition_str)?;
507+
}
508+
503509
if matches.opt_present("backup") {
504510
options.backup = true;
505511
}
@@ -553,6 +559,7 @@ impl CliOptions for GetOptsOptions {
553559
if let Some(error_on_unformatted) = self.error_on_unformatted {
554560
config.set().error_on_unformatted(error_on_unformatted);
555561
}
562+
config.set().edition(self.edition);
556563
if self.check {
557564
config.set().emit_mode(EmitMode::Diff);
558565
} else {
@@ -571,6 +578,14 @@ impl CliOptions for GetOptsOptions {
571578
}
572579
}
573580

581+
fn edition_from_edition_str(edition_str: &str) -> Result<Edition, failure::Error> {
582+
match edition_str {
583+
"2015" => Ok(Edition::Edition2015),
584+
"2018" => Ok(Edition::Edition2018),
585+
_ => Err(format_err!("Invalid value for `--edition`")),
586+
}
587+
}
588+
574589
fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode, failure::Error> {
575590
match emit_str {
576591
"files" => Ok(EmitMode::Files),

0 commit comments

Comments
 (0)