@@ -56,6 +56,10 @@ pub struct Opts {
56
56
/// Format all packages (only usable in workspaces)
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
{
@@ -544,6 +554,7 @@ mod cargo_fmt_tests {
544
554
assert_eq ! ( false , o. quiet) ;
545
555
assert_eq ! ( false , o. verbose) ;
546
556
assert_eq ! ( false , o. version) ;
557
+ assert_eq ! ( false , o. check) ;
547
558
assert_eq ! ( empty, o. packages) ;
548
559
assert_eq ! ( empty, o. rustfmt_options) ;
549
560
assert_eq ! ( false , o. format_all) ;
@@ -562,13 +573,15 @@ mod cargo_fmt_tests {
562
573
"p2" ,
563
574
"--message-format" ,
564
575
"short" ,
576
+ "--check" ,
565
577
"--" ,
566
578
"--edition" ,
567
579
"2018" ,
568
580
] ) ;
569
581
assert_eq ! ( true , o. quiet) ;
570
582
assert_eq ! ( false , o. verbose) ;
571
583
assert_eq ! ( false , o. version) ;
584
+ assert_eq ! ( true , o. check) ;
572
585
assert_eq ! ( vec![ "p1" , "p2" ] , o. packages) ;
573
586
assert_eq ! ( vec![ "--edition" , "2018" ] , o. rustfmt_options) ;
574
587
assert_eq ! ( false , o. format_all) ;
@@ -597,12 +610,12 @@ mod cargo_fmt_tests {
597
610
fn mandatory_separator ( ) {
598
611
assert ! (
599
612
Opts :: clap( )
600
- . get_matches_from_safe( & [ "test" , "--check " ] )
613
+ . get_matches_from_safe( & [ "test" , "--emit " ] )
601
614
. is_err( )
602
615
) ;
603
616
assert ! (
604
617
!Opts :: clap( )
605
- . get_matches_from_safe( & [ "test" , "--" , "--check " ] )
618
+ . get_matches_from_safe( & [ "test" , "--" , "--emit " ] )
606
619
. is_err( )
607
620
) ;
608
621
}
0 commit comments