]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/multiple_crate_versions.rs
Merge branch 'macro-use' into HEAD
[rust.git] / clippy_lints / src / multiple_crate_versions.rs
1 //! lint on multiple versions of a crate being used
2
3 use rustc::lint::*;
4 use rustc::{declare_lint, lint_array};
5 use syntax::ast::*;
6
7 use cargo_metadata;
8 use itertools::Itertools;
9
10 /// **What it does:** Checks to see if multiple versions of a crate are being
11 /// used.
12 ///
13 /// **Why is this bad?** This bloats the size of targets, and can lead to
14 /// confusing error messages when structs or traits are used interchangeably
15 /// between different versions of a crate.
16 ///
17 /// **Known problems:** Because this can be caused purely by the dependencies
18 /// themselves, it's not always possible to fix this issue.
19 ///
20 /// **Example:**
21 /// ```toml
22 /// # This will pull in both winapi v0.3.4 and v0.2.8, triggering a warning.
23 /// [dependencies]
24 /// ctrlc = "3.1.0"
25 /// ansi_term = "0.11.0"
26 /// ```
27 declare_clippy_lint! {
28     pub MULTIPLE_CRATE_VERSIONS,
29     cargo,
30     "multiple versions of the same crate being used"
31 }
32
33 pub struct Pass;
34
35 impl LintPass for Pass {
36     fn get_lints(&self) -> LintArray {
37         lint_array!(MULTIPLE_CRATE_VERSIONS)
38     }
39 }
40
41 impl EarlyLintPass for Pass {
42     fn check_crate(&mut self, cx: &EarlyContext, krate: &Crate) {
43         let metadata = match cargo_metadata::metadata_deps(None, true) {
44             Ok(metadata) => metadata,
45             Err(_) => {
46                 cx.span_lint(
47                     MULTIPLE_CRATE_VERSIONS,
48                     krate.span,
49                     "could not read cargo metadata"
50                 );
51
52                 return;
53             }
54         };
55
56         let mut packages = metadata.packages;
57         packages.sort_by(|a, b| a.name.cmp(&b.name));
58
59         for (name, group) in &packages.into_iter().group_by(|p| p.name.clone()) {
60             let group: Vec<cargo_metadata::Package> = group.collect();
61
62             if group.len() > 1 {
63                 let versions = group.into_iter().map(|p| p.version).join(", ");
64
65                 cx.span_lint(
66                     MULTIPLE_CRATE_VERSIONS,
67                     krate.span,
68                     &format!("multiple versions for dependency `{}`: {}", name, versions),
69                 );
70             }
71         }
72     }
73 }