1 //! lint on multiple versions of a crate being used
3 use crate::utils::{run_lints, span_lint};
4 use rustc_hir::{Crate, CRATE_HIR_ID};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 use rustc_span::source_map::DUMMY_SP;
9 use itertools::Itertools;
11 declare_clippy_lint! {
12 /// **What it does:** Checks to see if multiple versions of a crate are being
15 /// **Why is this bad?** This bloats the size of targets, and can lead to
16 /// confusing error messages when structs or traits are used interchangeably
17 /// between different versions of a crate.
19 /// **Known problems:** Because this can be caused purely by the dependencies
20 /// themselves, it's not always possible to fix this issue.
24 /// # This will pull in both winapi v0.3.x and v0.2.x, triggering a warning.
27 /// ansi_term = "=0.11.0"
29 pub MULTIPLE_CRATE_VERSIONS,
31 "multiple versions of the same crate being used"
34 declare_lint_pass!(MultipleCrateVersions => [MULTIPLE_CRATE_VERSIONS]);
36 impl LateLintPass<'_, '_> for MultipleCrateVersions {
37 fn check_crate(&mut self, cx: &LateContext<'_, '_>, _: &Crate<'_>) {
38 if !run_lints(cx, &[MULTIPLE_CRATE_VERSIONS], CRATE_HIR_ID) {
42 let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().exec() {
45 span_lint(cx, MULTIPLE_CRATE_VERSIONS, DUMMY_SP, "could not read cargo metadata");
50 let mut packages = metadata.packages;
51 packages.sort_by(|a, b| a.name.cmp(&b.name));
53 for (name, group) in &packages.into_iter().group_by(|p| p.name.clone()) {
54 let group: Vec<cargo_metadata::Package> = group.collect();
57 let versions = group.into_iter().map(|p| p.version).join(", ");
61 MULTIPLE_CRATE_VERSIONS,
63 &format!("multiple versions for dependency `{}`: {}", name, versions),