11use regex:: Regex ;
22use serde:: Deserialize ;
33use std:: fmt:: { self , Display , Formatter } ;
4- use std:: fs:: { remove_file, File } ;
4+ use std:: fs:: { self , remove_file, File } ;
55use std:: io:: Read ;
66use std:: path:: PathBuf ;
77use std:: process:: { self , Command } ;
88
99const RUSTC_COLOR_ARGS : & [ & str ] = & [ "--color" , "always" ] ;
1010const I_AM_DONE_REGEX : & str = r"(?m)^\s*///?\s*I\s+AM\s+NOT\s+DONE" ;
1111const CONTEXT : usize = 2 ;
12+ const CLIPPY_CARGO_TOML_PATH : & str = "./exercises/clippy/Cargo.toml" ;
1213
1314fn temp_file ( ) -> String {
1415 format ! ( "./temp_{}" , process:: id( ) )
@@ -19,6 +20,7 @@ fn temp_file() -> String {
1920pub enum Mode {
2021 Compile ,
2122 Test ,
23+ Clippy ,
2224}
2325
2426#[ derive( Deserialize ) ]
@@ -83,6 +85,34 @@ impl Exercise {
8385 . args ( & [ "--test" , self . path . to_str ( ) . unwrap ( ) , "-o" , & temp_file ( ) ] )
8486 . args ( RUSTC_COLOR_ARGS )
8587 . output ( ) ,
88+ Mode :: Clippy => {
89+ let cargo_toml = format ! (
90+ r#"[package]
91+ name = "{}"
92+ version = "0.0.1"
93+ edition = "2018"
94+ [[bin]]
95+ name = "{}"
96+ path = "{}.rs""# ,
97+ self . name, self . name, self . name
98+ ) ;
99+ fs:: write ( CLIPPY_CARGO_TOML_PATH , cargo_toml)
100+ . expect ( "Failed to write 📎 Clippy 📎 Cargo.toml file." ) ;
101+ // Due to an issue with Clippy, a cargo clean is required to catch all lints.
102+ // See https://github.com/rust-lang/rust-clippy/issues/2604
103+ // This is already fixed on master branch. See this issue to track merging into Cargo:
104+ // https://github.com/rust-lang/rust-clippy/issues/3837
105+ Command :: new ( "cargo" )
106+ . args ( & [ "clean" , "--manifest-path" , CLIPPY_CARGO_TOML_PATH ] )
107+ . args ( RUSTC_COLOR_ARGS )
108+ . output ( )
109+ . expect ( "Failed to run 'cargo clean'" ) ;
110+ Command :: new ( "cargo" )
111+ . args ( & [ "clippy" , "--manifest-path" , CLIPPY_CARGO_TOML_PATH ] )
112+ . args ( RUSTC_COLOR_ARGS )
113+ . args ( & [ "--" , "-D" , "warnings" ] )
114+ . output ( )
115+ }
86116 }
87117 . expect ( "Failed to run 'compile' command." ) ;
88118
0 commit comments