Skip to content

Commit 17cb2b1

Browse files
feat: add --check flag to cargo fmt (#3890)
1 parent 9d65b7d commit 17cb2b1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/cargo-fmt/main.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ pub struct Opts {
5656
/// Format all packages, and also their local path-based dependencies
5757
#[structopt(long = "all")]
5858
format_all: bool,
59+
60+
/// Run rustfmt in check mode
61+
#[structopt(long = "check")]
62+
check: bool,
5963
}
6064

6165
fn main() {
@@ -104,6 +108,12 @@ fn execute() -> i32 {
104108

105109
let strategy = CargoFmtStrategy::from_opts(&opts);
106110
let mut rustfmt_args = opts.rustfmt_options;
111+
if opts.check {
112+
let check_flag = String::from("--check");
113+
if !rustfmt_args.contains(&check_flag) {
114+
rustfmt_args.push(check_flag);
115+
}
116+
}
107117
if let Some(message_format) = opts.message_format {
108118
if let Err(msg) = convert_message_format_to_rustfmt_args(&message_format, &mut rustfmt_args)
109119
{
@@ -553,6 +563,7 @@ mod cargo_fmt_tests {
553563
assert_eq!(false, o.quiet);
554564
assert_eq!(false, o.verbose);
555565
assert_eq!(false, o.version);
566+
assert_eq!(false, o.check);
556567
assert_eq!(empty, o.packages);
557568
assert_eq!(empty, o.rustfmt_options);
558569
assert_eq!(false, o.format_all);
@@ -571,13 +582,15 @@ mod cargo_fmt_tests {
571582
"p2",
572583
"--message-format",
573584
"short",
585+
"--check",
574586
"--",
575587
"--edition",
576588
"2018",
577589
]);
578590
assert_eq!(true, o.quiet);
579591
assert_eq!(false, o.verbose);
580592
assert_eq!(false, o.version);
593+
assert_eq!(true, o.check);
581594
assert_eq!(vec!["p1", "p2"], o.packages);
582595
assert_eq!(vec!["--edition", "2018"], o.rustfmt_options);
583596
assert_eq!(false, o.format_all);
@@ -606,12 +619,12 @@ mod cargo_fmt_tests {
606619
fn mandatory_separator() {
607620
assert!(
608621
Opts::clap()
609-
.get_matches_from_safe(&["test", "--check"])
622+
.get_matches_from_safe(&["test", "--emit"])
610623
.is_err()
611624
);
612625
assert!(
613626
!Opts::clap()
614-
.get_matches_from_safe(&["test", "--", "--check"])
627+
.get_matches_from_safe(&["test", "--", "--emit"])
615628
.is_err()
616629
);
617630
}

0 commit comments

Comments
 (0)