]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/multiple_crate_versions.rs
Auto merge of #5638 - ebroto:issue_5628_add_suggestion_for_reversed_empty_ranges...
[rust.git] / clippy_lints / src / multiple_crate_versions.rs
1 //! lint on multiple versions of a crate being used
2
3 use crate::utils::{run_lints, span_lint};
4 use rustc_hir::def_id::LOCAL_CRATE;
5 use rustc_hir::{Crate, CRATE_HIR_ID};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::source_map::DUMMY_SP;
9
10 use cargo_metadata::{DependencyKind, MetadataCommand, Node, Package, PackageId};
11 use if_chain::if_chain;
12 use itertools::Itertools;
13
14 declare_clippy_lint! {
15     /// **What it does:** Checks to see if multiple versions of a crate are being
16     /// used.
17     ///
18     /// **Why is this bad?** This bloats the size of targets, and can lead to
19     /// confusing error messages when structs or traits are used interchangeably
20     /// between different versions of a crate.
21     ///
22     /// **Known problems:** Because this can be caused purely by the dependencies
23     /// themselves, it's not always possible to fix this issue.
24     ///
25     /// **Example:**
26     /// ```toml
27     /// # This will pull in both winapi v0.3.x and v0.2.x, triggering a warning.
28     /// [dependencies]
29     /// ctrlc = "=3.1.0"
30     /// ansi_term = "=0.11.0"
31     /// ```
32     pub MULTIPLE_CRATE_VERSIONS,
33     cargo,
34     "multiple versions of the same crate being used"
35 }
36
37 declare_lint_pass!(MultipleCrateVersions => [MULTIPLE_CRATE_VERSIONS]);
38
39 impl LateLintPass<'_, '_> for MultipleCrateVersions {
40     fn check_crate(&mut self, cx: &LateContext<'_, '_>, _: &Crate<'_>) {
41         if !run_lints(cx, &[MULTIPLE_CRATE_VERSIONS], CRATE_HIR_ID) {
42             return;
43         }
44
45         let metadata = if let Ok(metadata) = MetadataCommand::new().exec() {
46             metadata
47         } else {
48             span_lint(cx, MULTIPLE_CRATE_VERSIONS, DUMMY_SP, "could not read cargo metadata");
49             return;
50         };
51
52         let local_name = cx.tcx.crate_name(LOCAL_CRATE).as_str();
53         let mut packages = metadata.packages;
54         packages.sort_by(|a, b| a.name.cmp(&b.name));
55
56         if_chain! {
57             if let Some(resolve) = &metadata.resolve;
58             if let Some(local_id) = packages
59                 .iter()
60                 .find_map(|p| if p.name == *local_name { Some(&p.id) } else { None });
61             then {
62                 for (name, group) in &packages.iter().group_by(|p| p.name.clone()) {
63                     let group: Vec<&Package> = group.collect();
64
65                     if group.len() <= 1 {
66                         continue;
67                     }
68
69                     if group.iter().all(|p| is_normal_dep(&resolve.nodes, local_id, &p.id)) {
70                         let mut versions: Vec<_> = group.into_iter().map(|p| &p.version).collect();
71                         versions.sort();
72                         let versions = versions.iter().join(", ");
73
74                         span_lint(
75                             cx,
76                             MULTIPLE_CRATE_VERSIONS,
77                             DUMMY_SP,
78                             &format!("multiple versions for dependency `{}`: {}", name, versions),
79                         );
80                     }
81                 }
82             }
83         }
84     }
85 }
86
87 fn is_normal_dep(nodes: &[Node], local_id: &PackageId, dep_id: &PackageId) -> bool {
88     fn depends_on(node: &Node, dep_id: &PackageId) -> bool {
89         node.deps.iter().any(|dep| {
90             dep.pkg == *dep_id
91                 && dep
92                     .dep_kinds
93                     .iter()
94                     .any(|info| matches!(info.kind, DependencyKind::Normal))
95         })
96     }
97
98     nodes
99         .iter()
100         .filter(|node| depends_on(node, dep_id))
101         .any(|node| node.id == *local_id || is_normal_dep(nodes, local_id, &node.id))
102 }