@@ -17,14 +17,14 @@ use clap::ArgMatches;
1717use serde:: { Deserialize , Serialize } ;
1818use serde_json:: Value ;
1919
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
2121#[ derive( Debug , Serialize , Deserialize ) ]
22- struct CrateList {
22+ struct SourceList {
2323 crates : HashMap < String , TomlCrate > ,
2424}
2525
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
2828#[ derive( Debug , Serialize , Deserialize ) ]
2929struct TomlCrate {
3030 name : String ,
@@ -34,17 +34,16 @@ struct TomlCrate {
3434 path : Option < String > ,
3535}
3636
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`
3839#[ derive( Debug , Serialize , Deserialize , Eq , Hash , PartialEq ) ]
3940enum CrateSource {
4041 CratesIo { name : String , version : String } ,
4142 Git { name : String , url : String , commit : String } ,
4243 Path { name : String , path : PathBuf } ,
4344}
4445
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
4847#[ derive( Debug ) ]
4948struct Crate {
5049 version : String ,
@@ -53,6 +52,7 @@ struct Crate {
5352 path : PathBuf ,
5453}
5554
55+ /// A single warning that clippy issued while checking a `Crate`
5656#[ derive( Debug ) ]
5757struct ClippyWarning {
5858 crate_name : String ,
@@ -76,6 +76,9 @@ impl std::fmt::Display for ClippyWarning {
7676}
7777
7878impl 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
7982 fn download_and_extract ( & self ) -> Crate {
8083 match self {
8184 CrateSource :: CratesIo { name, version } => {
@@ -178,6 +181,8 @@ impl CrateSource {
178181}
179182
180183impl Crate {
184+ /// Run `cargo clippy` on the `Crate` and collect and return all the lint warnings that clippy
185+ /// issued
181186 fn run_clippy_lints ( & self , cargo_clippy_path : & PathBuf ) -> Vec < ClippyWarning > {
182187 println ! ( "Linting {} {}..." , & self . name, & self . version) ;
183188 let cargo_clippy_path = std:: fs:: canonicalize ( cargo_clippy_path) . unwrap ( ) ;
@@ -218,14 +223,15 @@ impl Crate {
218223 }
219224}
220225
226+ /// Builds clippy inside the repo to make sure we have a clippy executable we can use.
221227fn build_clippy ( ) {
222228 Command :: new ( "cargo" )
223229 . arg ( "build" )
224230 . output ( )
225231 . expect ( "Failed to build clippy!" ) ;
226232}
227233
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
229235fn read_crates ( toml_path : Option < & str > ) -> ( String , Vec < CrateSource > ) {
230236 let toml_path = PathBuf :: from (
231237 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>) {
234240 let toml_filename = toml_path. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
235241 let toml_content: String =
236242 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 =
238244 toml:: from_str ( & toml_content) . unwrap_or_else ( |e| panic ! ( "Failed to parse {}: \n {}" , toml_path. display( ) , e) ) ;
239245 // parse the hashmap of the toml file into a list of crates
240246 let tomlcrates: Vec < TomlCrate > = crate_list
@@ -288,7 +294,7 @@ fn read_crates(toml_path: Option<&str>) -> (String, Vec<CrateSource>) {
288294 ( toml_filename, crate_sources)
289295}
290296
291- // extract interesting data from a json lint message
297+ /// Parse the json output of clippy and return a `ClippyWarning`
292298fn parse_json_message ( json_message : & str , krate : & Crate ) -> ClippyWarning {
293299 let jmsg: Value = serde_json:: from_str ( & json_message) . unwrap_or_else ( |e| panic ! ( "Failed to parse json:\n {:?}" , e) ) ;
294300
@@ -313,7 +319,7 @@ fn parse_json_message(json_message: &str, krate: &Crate) -> ClippyWarning {
313319 }
314320}
315321
316- // the main fn
322+ /// lintchecks ` main()` function
317323pub fn run ( clap_config : & ArgMatches ) {
318324 let cargo_clippy_path: PathBuf = PathBuf :: from ( "target/debug/cargo-clippy" ) ;
319325
0 commit comments