Skip to content

Commit e3a374a

Browse files
committed
Fix alexcrichton's comments
1 parent 50876d1 commit e3a374a

File tree

1 file changed

+77
-95
lines changed

1 file changed

+77
-95
lines changed

src/tools/tidy/src/deps.rs

Lines changed: 77 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Check license of third-party deps by inspecting src/vendor
1212
13-
use std::collections::HashSet;
13+
use std::collections::{BTreeSet, HashSet};
1414
use std::fs::File;
1515
use std::io::Read;
1616
use std::path::Path;
@@ -49,90 +49,72 @@ static EXCEPTIONS: &'static [&'static str] = &[
4949
];
5050

5151
/// Which crates to check against the whitelist?
52-
static WHITELIST_CRATES: &'static [Crate] =
53-
&[Crate("rustc", "0.0.0"), Crate("rustc_trans", "0.0.0")];
52+
static WHITELIST_CRATES: &'static [Crate] = &[Crate("rustc"), Crate("rustc_trans")];
5453

5554
/// Whitelist of crates rustc is allowed to depend on. Avoid adding to the list if possible.
5655
static WHITELIST: &'static [Crate] = &[
57-
// Crate("ar", "0.3.1"),
58-
// Crate("arena", "0.0.0"),
59-
// Crate("backtrace", "0.3.5"),
60-
// Crate("backtrace-sys", "0.1.16"),
61-
// Crate("bitflags", "1.0.1"),
62-
// Crate("build_helper", "0.1.0"),
63-
// Crate("byteorder", "1.2.1"),
64-
// Crate("cc", "1.0.4"),
65-
// Crate("cfg-if", "0.1.2"),
66-
// Crate("cmake", "0.1.29"),
67-
// Crate("filetime", "0.1.15"),
68-
// Crate("flate2", "1.0.1"),
69-
// Crate("fmt_macros", "0.0.0"),
70-
// Crate("fuchsia-zircon", "0.3.3"),
71-
// Crate("fuchsia-zircon-sys", "0.3.3"),
72-
// Crate("graphviz", "0.0.0"),
73-
// Crate("jobserver", "0.1.9"),
74-
// Crate("kernel32-sys", "0.2.2"),
75-
// Crate("lazy_static", "0.2.11"),
76-
// Crate("libc", "0.2.36"),
77-
// Crate("log", "0.4.1"),
78-
// Crate("log_settings", "0.1.1"),
79-
// Crate("miniz-sys", "0.1.10"),
80-
// Crate("num_cpus", "1.8.0"),
81-
// Crate("owning_ref", "0.3.3"),
82-
// Crate("parking_lot", "0.5.3"),
83-
// Crate("parking_lot_core", "0.2.9"),
84-
// Crate("rand", "0.3.20"),
85-
// Crate("redox_syscall", "0.1.37"),
86-
// Crate("rustc", "0.0.0"),
87-
// Crate("rustc-demangle", "0.1.5"),
88-
// Crate("rustc_allocator", "0.0.0"),
89-
// Crate("rustc_apfloat", "0.0.0"),
90-
// Crate("rustc_back", "0.0.0"),
91-
// Crate("rustc_binaryen", "0.0.0"),
92-
// Crate("rustc_const_eval", "0.0.0"),
93-
// Crate("rustc_const_math", "0.0.0"),
94-
// Crate("rustc_cratesio_shim", "0.0.0"),
95-
// Crate("rustc_data_structures", "0.0.0"),
96-
// Crate("rustc_errors", "0.0.0"),
97-
// Crate("rustc_incremental", "0.0.0"),
98-
// Crate("rustc_llvm", "0.0.0"),
99-
// Crate("rustc_mir", "0.0.0"),
100-
// Crate("rustc_platform_intrinsics", "0.0.0"),
101-
// Crate("rustc_trans", "0.0.0"),
102-
// Crate("rustc_trans_utils", "0.0.0"),
103-
// Crate("serialize", "0.0.0"),
104-
// Crate("smallvec", "0.6.0"),
105-
// Crate("stable_deref_trait", "1.0.0"),
106-
// Crate("syntax", "0.0.0"),
107-
// Crate("syntax_pos", "0.0.0"),
108-
// Crate("tempdir", "0.3.5"),
109-
// Crate("unicode-width", "0.1.4"),
110-
// Crate("winapi", "0.2.8"),
111-
// Crate("winapi", "0.3.4"),
112-
// Crate("winapi-build", "0.1.1"),
113-
// Crate("winapi-i686-pc-windows-gnu", "0.4.0"),
114-
// Crate("winapi-x86_64-pc-windows-gnu", "0.4.0"),
56+
// Crate("ar "),
57+
// Crate("arena "),
58+
// Crate("backtrace "),
59+
// Crate("backtrace-sys "),
60+
// Crate("bitflags "),
61+
// Crate("build_helper "),
62+
// Crate("byteorder "),
63+
// Crate("cc "),
64+
// Crate("cfg-if "),
65+
// Crate("cmake "),
66+
// Crate("filetime "),
67+
// Crate("flate2 "),
68+
// Crate("fmt_macros "),
69+
// Crate("fuchsia-zircon "),
70+
// Crate("fuchsia-zircon-sys "),
71+
// Crate("graphviz "),
72+
// Crate("jobserver "),
73+
// Crate("kernel32-sys "),
74+
// Crate("lazy_static "),
75+
// Crate("libc "),
76+
// Crate("log "),
77+
// Crate("log_settings "),
78+
// Crate("miniz-sys "),
79+
// Crate("num_cpus "),
80+
// Crate("owning_ref "),
81+
// Crate("parking_lot "),
82+
// Crate("parking_lot_core "),
83+
// Crate("rand "),
84+
// Crate("redox_syscall "),
85+
// Crate("rustc "),
86+
// Crate("rustc-demangle "),
87+
// Crate("rustc_allocator "),
88+
// Crate("rustc_apfloat "),
89+
// Crate("rustc_back "),
90+
// Crate("rustc_binaryen "),
91+
// Crate("rustc_const_eval "),
92+
// Crate("rustc_const_math "),
93+
// Crate("rustc_cratesio_shim "),
94+
// Crate("rustc_data_structures "),
95+
// Crate("rustc_errors "),
96+
// Crate("rustc_incremental "),
97+
// Crate("rustc_llvm "),
98+
// Crate("rustc_mir "),
99+
// Crate("rustc_platform_intrinsics "),
100+
// Crate("rustc_trans "),
101+
// Crate("rustc_trans_utils "),
102+
// Crate("serialize "),
103+
// Crate("smallvec "),
104+
// Crate("stable_deref_trait "),
105+
// Crate("syntax "),
106+
// Crate("syntax_pos "),
107+
// Crate("tempdir "),
108+
// Crate("unicode-width "),
109+
// Crate("winapi "),
110+
// Crate("winapi-build"),
115111
];
116112

117113
// Some types for Serde to deserialize the output of `cargo metadata` to...
118114

119115
#[derive(Deserialize)]
120116
struct Output {
121117
resolve: Resolve,
122-
123-
// Not used, but needed to not confuse serde :P
124-
#[allow(dead_code)] packages: Vec<Package>,
125-
}
126-
127-
// Not used, but needed to not confuse serde :P
128-
#[allow(dead_code)]
129-
#[derive(Deserialize)]
130-
struct Package {
131-
name: String,
132-
version: String,
133-
id: String,
134-
source: Option<String>,
135-
manifest_path: String,
136118
}
137119

138120
#[derive(Deserialize)]
@@ -148,19 +130,18 @@ struct ResolveNode {
148130

149131
/// A unique identifier for a crate
150132
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Hash)]
151-
struct Crate<'a>(&'a str, &'a str); // (name, version)
133+
struct Crate<'a>(&'a str); // (name,)
152134

153135
impl<'a> Crate<'a> {
154136
pub fn from_str(s: &'a str) -> Self {
155137
let mut parts = s.split(" ");
156138
let name = parts.next().unwrap();
157-
let version = parts.next().unwrap();
158139

159-
Crate(name, version)
140+
Crate(name)
160141
}
161142

162143
pub fn id_str(&self) -> String {
163-
format!("{} {}", self.0, self.1)
144+
format!("{} ", self.0)
164145
}
165146
}
166147

@@ -204,20 +185,17 @@ pub fn check_whitelist(path: &Path, cargo: &Path, bad: &mut bool) {
204185
let whitelist: HashSet<_> = WHITELIST.iter().cloned().collect();
205186

206187
// Check dependencies
207-
let mut unapproved = Vec::new();
188+
let mut visited = BTreeSet::new();
189+
let mut unapproved = BTreeSet::new();
208190
for &krate in WHITELIST_CRATES.iter() {
209-
let mut bad = check_crate_whitelist(&whitelist, &resolve, krate);
191+
let mut bad = check_crate_whitelist(&whitelist, &resolve, &mut visited, krate);
210192
unapproved.append(&mut bad);
211193
}
212194

213-
// For ease of reading
214-
unapproved.sort_unstable();
215-
unapproved.dedup();
216-
217195
if unapproved.len() > 0 {
218196
println!("Dependencies not on the whitelist:");
219197
for dep in unapproved {
220-
println!("* {} {}", dep.0, dep.1); // name version
198+
println!("* {}", dep.id_str());
221199
}
222200
*bad = true;
223201
}
@@ -282,17 +260,25 @@ fn get_deps(path: &Path, cargo: &Path) -> Resolve {
282260

283261
/// Checks the dependencies of the given crate from the given cargo metadata to see if they are on
284262
/// the whitelist. Returns a list of illegal dependencies.
285-
fn check_crate_whitelist<'a>(
263+
fn check_crate_whitelist<'a, 'b>(
286264
whitelist: &'a HashSet<Crate>,
287265
resolve: &'a Resolve,
266+
visited: &'b mut BTreeSet<Crate<'a>>,
288267
krate: Crate<'a>,
289-
) -> Vec<Crate<'a>> {
268+
) -> BTreeSet<Crate<'a>> {
290269
// Will contain bad deps
291-
let mut unapproved = Vec::new();
270+
let mut unapproved = BTreeSet::new();
271+
272+
// Check if we have already visited this crate
273+
if visited.contains(&krate) {
274+
return unapproved;
275+
}
276+
277+
visited.insert(krate);
292278

293279
// If this dependency is not on the WHITELIST, add to bad set
294280
if !whitelist.contains(&krate) {
295-
unapproved.push(krate);
281+
unapproved.insert(krate);
296282
}
297283

298284
// Do a DFS in the crate graph (it's a DAG, so we know we have no cycles!)
@@ -304,14 +290,10 @@ fn check_crate_whitelist<'a>(
304290

305291
for dep in to_check.dependencies.iter() {
306292
let krate = Crate::from_str(dep);
307-
let mut bad = check_crate_whitelist(whitelist, resolve, krate);
293+
let mut bad = check_crate_whitelist(whitelist, resolve, visited, krate);
308294

309295
unapproved.append(&mut bad);
310296
}
311297

312-
// Remove duplicates
313-
unapproved.sort_unstable();
314-
unapproved.dedup();
315-
316298
unapproved
317299
}

0 commit comments

Comments
 (0)