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