@@ -56,6 +56,10 @@ pub struct Opts {
56
56
/// Format all packages, and also their local path-based dependencies
57
57
#[ structopt( long = "all" ) ]
58
58
format_all : bool ,
59
+
60
+ /// Run rustfmt in check mode
61
+ #[ structopt( long = "check" ) ]
62
+ check : bool ,
59
63
}
60
64
61
65
fn main ( ) {
@@ -104,6 +108,12 @@ fn execute() -> i32 {
104
108
105
109
let strategy = CargoFmtStrategy :: from_opts ( & opts) ;
106
110
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
+ }
107
117
if let Some ( message_format) = opts. message_format {
108
118
if let Err ( msg) = convert_message_format_to_rustfmt_args ( & message_format, & mut rustfmt_args)
109
119
{
@@ -553,6 +563,7 @@ mod cargo_fmt_tests {
553
563
assert_eq ! ( false , o. quiet) ;
554
564
assert_eq ! ( false , o. verbose) ;
555
565
assert_eq ! ( false , o. version) ;
566
+ assert_eq ! ( false , o. check) ;
556
567
assert_eq ! ( empty, o. packages) ;
557
568
assert_eq ! ( empty, o. rustfmt_options) ;
558
569
assert_eq ! ( false , o. format_all) ;
@@ -571,13 +582,15 @@ mod cargo_fmt_tests {
571
582
"p2" ,
572
583
"--message-format" ,
573
584
"short" ,
585
+ "--check" ,
574
586
"--" ,
575
587
"--edition" ,
576
588
"2018" ,
577
589
] ) ;
578
590
assert_eq ! ( true , o. quiet) ;
579
591
assert_eq ! ( false , o. verbose) ;
580
592
assert_eq ! ( false , o. version) ;
593
+ assert_eq ! ( true , o. check) ;
581
594
assert_eq ! ( vec![ "p1" , "p2" ] , o. packages) ;
582
595
assert_eq ! ( vec![ "--edition" , "2018" ] , o. rustfmt_options) ;
583
596
assert_eq ! ( false , o. format_all) ;
@@ -606,12 +619,12 @@ mod cargo_fmt_tests {
606
619
fn mandatory_separator ( ) {
607
620
assert ! (
608
621
Opts :: clap( )
609
- . get_matches_from_safe( & [ "test" , "--check " ] )
622
+ . get_matches_from_safe( & [ "test" , "--emit " ] )
610
623
. is_err( )
611
624
) ;
612
625
assert ! (
613
626
!Opts :: clap( )
614
- . get_matches_from_safe( & [ "test" , "--" , "--check " ] )
627
+ . get_matches_from_safe( & [ "test" , "--" , "--emit " ] )
615
628
. is_err( )
616
629
) ;
617
630
}
0 commit comments