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