@@ -17,14 +17,14 @@ use clap::ArgMatches;
17
17
use serde:: { Deserialize , Serialize } ;
18
18
use serde_json:: Value ;
19
19
20
- // use this to store the crates when interacting with the crates .toml file
20
+ /// List of sources to check, loaded from a .toml file
21
21
#[ derive( Debug , Serialize , Deserialize ) ]
22
- struct CrateList {
22
+ struct SourceList {
23
23
crates : HashMap < String , TomlCrate > ,
24
24
}
25
25
26
- // crate data we stored in the toml, can have multiple versions per crate
27
- // A single TomlCrate is laster mapped to several CrateSources in that case
26
+ /// A crate source stored inside the . toml
27
+ /// will be translated into on one of the `CrateSource` variants
28
28
#[ derive( Debug , Serialize , Deserialize ) ]
29
29
struct TomlCrate {
30
30
name : String ,
@@ -34,17 +34,16 @@ struct TomlCrate {
34
34
path : Option < String > ,
35
35
}
36
36
37
- // represents an archive we download from crates.io, or a git repo, or a local repo
37
+ /// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
38
+ /// Once processed (downloaded/extracted/cloned/copied...), this will be translated into a `Crate`
38
39
#[ derive( Debug , Serialize , Deserialize , Eq , Hash , PartialEq ) ]
39
40
enum CrateSource {
40
41
CratesIo { name : String , version : String } ,
41
42
Git { name : String , url : String , commit : String } ,
42
43
Path { name : String , path : PathBuf } ,
43
44
}
44
45
45
- // represents the extracted sourcecode of a crate
46
- // we actually don't need to special-case git repos here because it does not matter for clippy, yay!
47
- // (clippy only needs a simple path)
46
+ /// Represents the actual source code of a crate that we ran "cargo clippy" on
48
47
#[ derive( Debug ) ]
49
48
struct Crate {
50
49
version : String ,
@@ -53,6 +52,7 @@ struct Crate {
53
52
path : PathBuf ,
54
53
}
55
54
55
+ /// A single warning that clippy issued while checking a `Crate`
56
56
#[ derive( Debug ) ]
57
57
struct ClippyWarning {
58
58
crate_name : String ,
@@ -76,6 +76,9 @@ impl std::fmt::Display for ClippyWarning {
76
76
}
77
77
78
78
impl CrateSource {
79
+ /// Makes the sources available on the disk for clippy to check.
80
+ /// Clones a git repo and checks out the specified commit or downloads a crate from crates.io or
81
+ /// copies a local folder
79
82
fn download_and_extract ( & self ) -> Crate {
80
83
match self {
81
84
CrateSource :: CratesIo { name, version } => {
@@ -178,6 +181,8 @@ impl CrateSource {
178
181
}
179
182
180
183
impl Crate {
184
+ /// Run `cargo clippy` on the `Crate` and collect and return all the lint warnings that clippy
185
+ /// issued
181
186
fn run_clippy_lints ( & self , cargo_clippy_path : & PathBuf ) -> Vec < ClippyWarning > {
182
187
println ! ( "Linting {} {}..." , & self . name, & self . version) ;
183
188
let cargo_clippy_path = std:: fs:: canonicalize ( cargo_clippy_path) . unwrap ( ) ;
@@ -218,14 +223,15 @@ impl Crate {
218
223
}
219
224
}
220
225
226
+ /// Builds clippy inside the repo to make sure we have a clippy executable we can use.
221
227
fn build_clippy ( ) {
222
228
Command :: new ( "cargo" )
223
229
. arg ( "build" )
224
230
. output ( )
225
231
. expect ( "Failed to build clippy!" ) ;
226
232
}
227
233
228
- // get a list of CrateSources we want to check from a "lintcheck_crates.toml" file.
234
+ /// Read a `toml` file and return a list of ` CrateSources` that we want to check with clippy
229
235
fn read_crates ( toml_path : Option < & str > ) -> ( String , Vec < CrateSource > ) {
230
236
let toml_path = PathBuf :: from (
231
237
env:: var ( "LINTCHECK_TOML" ) . unwrap_or ( toml_path. unwrap_or ( "clippy_dev/lintcheck_crates.toml" ) . to_string ( ) ) ,
@@ -234,7 +240,7 @@ fn read_crates(toml_path: Option<&str>) -> (String, Vec<CrateSource>) {
234
240
let toml_filename = toml_path. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
235
241
let toml_content: String =
236
242
std:: fs:: read_to_string ( & toml_path) . unwrap_or_else ( |_| panic ! ( "Failed to read {}" , toml_path. display( ) ) ) ;
237
- let crate_list: CrateList =
243
+ let crate_list: SourceList =
238
244
toml:: from_str ( & toml_content) . unwrap_or_else ( |e| panic ! ( "Failed to parse {}: \n {}" , toml_path. display( ) , e) ) ;
239
245
// parse the hashmap of the toml file into a list of crates
240
246
let tomlcrates: Vec < TomlCrate > = crate_list
@@ -288,7 +294,7 @@ fn read_crates(toml_path: Option<&str>) -> (String, Vec<CrateSource>) {
288
294
( toml_filename, crate_sources)
289
295
}
290
296
291
- // extract interesting data from a json lint message
297
+ /// Parse the json output of clippy and return a `ClippyWarning`
292
298
fn parse_json_message ( json_message : & str , krate : & Crate ) -> ClippyWarning {
293
299
let jmsg: Value = serde_json:: from_str ( & json_message) . unwrap_or_else ( |e| panic ! ( "Failed to parse json:\n {:?}" , e) ) ;
294
300
@@ -313,7 +319,7 @@ fn parse_json_message(json_message: &str, krate: &Crate) -> ClippyWarning {
313
319
}
314
320
}
315
321
316
- // the main fn
322
+ /// lintchecks ` main()` function
317
323
pub fn run ( clap_config : & ArgMatches ) {
318
324
let cargo_clippy_path: PathBuf = PathBuf :: from ( "target/debug/cargo-clippy" ) ;
319
325
0 commit comments