1
1
use regex:: Regex ;
2
2
use serde:: Deserialize ;
3
3
use std:: fmt:: { self , Display , Formatter } ;
4
- use std:: fs:: { remove_file, File } ;
4
+ use std:: fs:: { self , remove_file, File } ;
5
5
use std:: io:: Read ;
6
6
use std:: path:: PathBuf ;
7
7
use std:: process:: { self , Command } ;
8
8
9
9
const RUSTC_COLOR_ARGS : & [ & str ] = & [ "--color" , "always" ] ;
10
10
const I_AM_DONE_REGEX : & str = r"(?m)^\s*///?\s*I\s+AM\s+NOT\s+DONE" ;
11
11
const CONTEXT : usize = 2 ;
12
+ const CLIPPY_CARGO_TOML_PATH : & str = "./exercises/clippy/Cargo.toml" ;
12
13
13
14
fn temp_file ( ) -> String {
14
15
format ! ( "./temp_{}" , process:: id( ) )
@@ -19,6 +20,7 @@ fn temp_file() -> String {
19
20
pub enum Mode {
20
21
Compile ,
21
22
Test ,
23
+ Clippy ,
22
24
}
23
25
24
26
#[ derive( Deserialize ) ]
@@ -83,6 +85,34 @@ impl Exercise {
83
85
. args ( & [ "--test" , self . path . to_str ( ) . unwrap ( ) , "-o" , & temp_file ( ) ] )
84
86
. args ( RUSTC_COLOR_ARGS )
85
87
. 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
+ }
86
116
}
87
117
. expect ( "Failed to run 'compile' command." ) ;
88
118
0 commit comments