]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/multiple_crate_versions.rs
Merge pull request #3285 from devonhollowood/pedantic-dogfood-items-after-statements
[rust.git] / clippy_lints / src / multiple_crate_versions.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 //! lint on multiple versions of a crate being used
12
13 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::syntax::ast::*;
16 use crate::utils::span_lint;
17
18 use cargo_metadata;
19 use itertools::Itertools;
20
21 /// **What it does:** Checks to see if multiple versions of a crate are being
22 /// used.
23 ///
24 /// **Why is this bad?** This bloats the size of targets, and can lead to
25 /// confusing error messages when structs or traits are used interchangeably
26 /// between different versions of a crate.
27 ///
28 /// **Known problems:** Because this can be caused purely by the dependencies
29 /// themselves, it's not always possible to fix this issue.
30 ///
31 /// **Example:**
32 /// ```toml
33 /// # This will pull in both winapi v0.3.4 and v0.2.8, triggering a warning.
34 /// [dependencies]
35 /// ctrlc = "3.1.0"
36 /// ansi_term = "0.11.0"
37 /// ```
38 declare_clippy_lint! {
39     pub MULTIPLE_CRATE_VERSIONS,
40     cargo,
41     "multiple versions of the same crate being used"
42 }
43
44 pub struct Pass;
45
46 impl LintPass for Pass {
47     fn get_lints(&self) -> LintArray {
48         lint_array!(MULTIPLE_CRATE_VERSIONS)
49     }
50 }
51
52 impl EarlyLintPass for Pass {
53     fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
54         let metadata = if let Ok(metadata) = cargo_metadata::metadata_deps(None, true) {
55             metadata
56         } else {
57             span_lint(
58                 cx,
59                 MULTIPLE_CRATE_VERSIONS,
60                 krate.span,
61                 "could not read cargo metadata"
62             );
63
64             return;
65         };
66
67         let mut packages = metadata.packages;
68         packages.sort_by(|a, b| a.name.cmp(&b.name));
69
70         for (name, group) in &packages.into_iter().group_by(|p| p.name.clone()) {
71             let group: Vec<cargo_metadata::Package> = group.collect();
72
73             if group.len() > 1 {
74                 let versions = group.into_iter().map(|p| p.version).join(", ");
75
76                 span_lint(
77                     cx,
78                     MULTIPLE_CRATE_VERSIONS,
79                     krate.span,
80                     &format!("multiple versions for dependency `{}`: {}", name, versions),
81                 );
82             }
83         }
84     }
85 }