Skip to content

Commit 0a9ec7e

Browse files
feat: add --check flag to cargo fmt
1 parent ed7c032 commit 0a9ec7e

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 (only usable in workspaces)
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
{
@@ -544,6 +554,7 @@ mod cargo_fmt_tests {
544554
assert_eq!(false, o.quiet);
545555
assert_eq!(false, o.verbose);
546556
assert_eq!(false, o.version);
557+
assert_eq!(false, o.check);
547558
assert_eq!(empty, o.packages);
548559
assert_eq!(empty, o.rustfmt_options);
549560
assert_eq!(false, o.format_all);
@@ -562,13 +573,15 @@ mod cargo_fmt_tests {
562573
"p2",
563574
"--message-format",
564575
"short",
576+
"--check",
565577
"--",
566578
"--edition",
567579
"2018",
568580
]);
569581
assert_eq!(true, o.quiet);
570582
assert_eq!(false, o.verbose);
571583
assert_eq!(false, o.version);
584+
assert_eq!(true, o.check);
572585
assert_eq!(vec!["p1", "p2"], o.packages);
573586
assert_eq!(vec!["--edition", "2018"], o.rustfmt_options);
574587
assert_eq!(false, o.format_all);
@@ -597,12 +610,12 @@ mod cargo_fmt_tests {
597610
fn mandatory_separator() {
598611
assert!(
599612
Opts::clap()
600-
.get_matches_from_safe(&["test", "--check"])
613+
.get_matches_from_safe(&["test", "--emit"])
601614
.is_err()
602615
);
603616
assert!(
604617
!Opts::clap()
605-
.get_matches_from_safe(&["test", "--", "--check"])
618+
.get_matches_from_safe(&["test", "--", "--emit"])
606619
.is_err()
607620
);
608621
}

0 commit comments

Comments
 (0)