]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/multiple_crate_versions.rs
multiple_crate_versions: skip dev and build deps
[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     #[allow(clippy::find_map)]
41     fn check_crate(&mut self, cx: &LateContext<'_, '_>, _: &Crate<'_>) {
42         if !run_lints(cx, &[MULTIPLE_CRATE_VERSIONS], CRATE_HIR_ID) {
43             return;
44         }
45
46         let metadata = if let Ok(metadata) = MetadataCommand::new().exec() {
47             metadata
48         } else {
49             span_lint(cx, MULTIPLE_CRATE_VERSIONS, DUMMY_SP, "could not read cargo metadata");
50             return;
51         };
52
53         let local_name = cx.tcx.crate_name(LOCAL_CRATE).as_str();
54         let mut packages = metadata.packages;
55         packages.sort_by(|a, b| a.name.cmp(&b.name));
56
57         if_chain! {
58             if let Some(resolve) = &metadata.resolve;
59             if let Some(local_id) = packages.iter().find(|p| p.name == *local_name).map(|p| &p.id);
60             then {
61                 for (name, group) in &packages.iter().group_by(|p| p.name.clone()) {
62                     let group: Vec<&Package> = group.collect();
63
64                     if group.len() <= 1 {
65                         continue;
66                     }
67
68                     if group.iter().all(|p| is_normal_dep(&resolve.nodes, local_id, &p.id)) {
69                         let mut versions: Vec<_> = group.into_iter().map(|p| &p.version).collect();
70                         versions.sort();
71                         let versions = versions.iter().join(", ");
72
73                         span_lint(
74                             cx,
75                             MULTIPLE_CRATE_VERSIONS,
76                             DUMMY_SP,
77                             &format!("multiple versions for dependency `{}`: {}", name, versions),
78                         );
79                     }
80                 }
81             }
82         }
83     }
84 }
85
86 fn is_normal_dep(nodes: &[Node], local_id: &PackageId, dep_id: &PackageId) -> bool {
87     fn depends_on(node: &Node, dep_id: &PackageId) -> bool {
88         node.deps.iter().any(|dep| {
89             dep.pkg == *dep_id
90                 && dep
91                     .dep_kinds
92                     .iter()
93                     .any(|info| matches!(info.kind, DependencyKind::Normal))
94         })
95     }
96
97     nodes
98         .iter()
99         .filter(|node| depends_on(node, dep_id))
100         .any(|node| node.id == *local_id || is_normal_dep(nodes, local_id, &node.id))
101 }