Skip to content

Commit 4d9de48

Browse files
committed
Add --check flag.
cc #1976
1 parent 798bffb commit 4d9de48

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/bin/main.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ fn make_opts() -> Options {
9191
let mut opts = Options::new();
9292

9393
// Sorted in alphabetical order.
94+
opts.optflag(
95+
"",
96+
"check",
97+
"Run in 'check' mode. Exits with 0 if input if formatted correctly. Exits \
98+
with 1 and prints a diff if formatting is required.",
99+
);
94100
opts.optopt(
95101
"",
96102
"color",
@@ -308,7 +314,8 @@ fn print_usage_to_stdout(opts: &Options, reason: &str) {
308314
}
309315

310316
fn print_help_file_lines() {
311-
println!("If you want to restrict reformatting to specific sets of lines, you can
317+
println!(
318+
"If you want to restrict reformatting to specific sets of lines, you can
312319
use the `--file-lines` option. Its argument is a JSON array of objects
313320
with `file` and `range` properties, where `file` is a file name, and
314321
`range` is an array representing a range of lines like `[7,13]`. Ranges
@@ -325,7 +332,8 @@ rustfmt --file-lines '[
325332
326333
would format lines `7-13` and `21-29` of `src/lib.rs`, and lines `10-11`,
327334
and `15` of `src/foo.rs`. No other files would be formatted, even if they
328-
are included as out of line modules from `src/lib.rs`.");
335+
are included as out of line modules from `src/lib.rs`."
336+
);
329337
}
330338

331339
fn print_version() {

src/config/options.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ pub struct CliOptions {
332332
verbose: bool,
333333
pub(super) config_path: Option<PathBuf>,
334334
write_mode: Option<WriteMode>,
335+
check: bool,
335336
color: Option<Color>,
336337
file_lines: FileLines, // Default is all lines in all files.
337338
unstable_features: bool,
@@ -361,7 +362,11 @@ impl CliOptions {
361362

362363
options.config_path = matches.opt_str("config-path").map(PathBuf::from);
363364

365+
options.check = matches.opt_present("check");
364366
if let Some(ref write_mode) = matches.opt_str("write-mode") {
367+
if options.check {
368+
return Err(format_err!("Invalid to set write-mode and `--check`"));
369+
}
365370
if let Ok(write_mode) = WriteMode::from_str(write_mode) {
366371
options.write_mode = Some(write_mode);
367372
} else {
@@ -410,7 +415,9 @@ impl CliOptions {
410415
if let Some(error_on_unformatted) = self.error_on_unformatted {
411416
config.set().error_on_unformatted(error_on_unformatted);
412417
}
413-
if let Some(write_mode) = self.write_mode {
418+
if self.check {
419+
config.set().write_mode(WriteMode::Check);
420+
} else if let Some(write_mode) = self.write_mode {
414421
config.set().write_mode(write_mode);
415422
}
416423
if let Some(color) = self.color {

src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ pub use config::{file_lines, load_config, Config, Verbosity, WriteMode};
6868

6969
pub type FmtResult<T> = std::result::Result<T, failure::Error>;
7070

71-
pub const WRITE_MODE_LIST: &str =
72-
"[replace|overwrite|display|plain|diff|coverage|checkstyle|check]";
71+
pub const WRITE_MODE_LIST: &str = "[replace|overwrite|display|plain|diff|coverage|checkstyle]";
7372

7473
#[macro_use]
7574
mod utils;

0 commit comments

Comments
 (0)