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