1 //! lint on multiple versions of a crate being used
3 use crate::utils::span_lint;
4 use rustc::declare_lint_pass;
5 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
6 use rustc_session::declare_tool_lint;
7 use syntax::{ast::*, source_map::DUMMY_SP};
10 use itertools::Itertools;
12 declare_clippy_lint! {
13 /// **What it does:** Checks to see if multiple versions of a crate are being
16 /// **Why is this bad?** This bloats the size of targets, and can lead to
17 /// confusing error messages when structs or traits are used interchangeably
18 /// between different versions of a crate.
20 /// **Known problems:** Because this can be caused purely by the dependencies
21 /// themselves, it's not always possible to fix this issue.
25 /// # This will pull in both winapi v0.3.4 and v0.2.8, triggering a warning.
28 /// ansi_term = "0.11.0"
30 pub MULTIPLE_CRATE_VERSIONS,
32 "multiple versions of the same crate being used"
35 declare_lint_pass!(MultipleCrateVersions => [MULTIPLE_CRATE_VERSIONS]);
37 impl EarlyLintPass for MultipleCrateVersions {
38 fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
39 let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().exec() {
42 span_lint(cx, MULTIPLE_CRATE_VERSIONS, DUMMY_SP, "could not read cargo metadata");
47 let mut packages = metadata.packages;
48 packages.sort_by(|a, b| a.name.cmp(&b.name));
50 for (name, group) in &packages.into_iter().group_by(|p| p.name.clone()) {
51 let group: Vec<cargo_metadata::Package> = group.collect();
54 let versions = group.into_iter().map(|p| p.version).join(", ");
58 MULTIPLE_CRATE_VERSIONS,
60 &format!("multiple versions for dependency `{}`: {}", name, versions),