1 //! lint on multiple versions of a crate being used
3 use crate::utils::span_lint;
4 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
6 use syntax::{ast::*, 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.4 and v0.2.8, triggering a warning.
27 /// ansi_term = "0.11.0"
29 pub MULTIPLE_CRATE_VERSIONS,
31 "multiple versions of the same crate being used"
36 impl LintPass for Pass {
37 fn get_lints(&self) -> LintArray {
38 lint_array!(MULTIPLE_CRATE_VERSIONS)
41 fn name(&self) -> &'static str {
42 "MultipleCrateVersions"
46 impl EarlyLintPass for Pass {
47 fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
48 let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().exec() {
51 span_lint(cx, MULTIPLE_CRATE_VERSIONS, DUMMY_SP, "could not read cargo metadata");
56 let mut packages = metadata.packages;
57 packages.sort_by(|a, b| a.name.cmp(&b.name));
59 for (name, group) in &packages.into_iter().group_by(|p| p.name.clone()) {
60 let group: Vec<cargo_metadata::Package> = group.collect();
63 let versions = group.into_iter().map(|p| p.version).join(", ");
67 MULTIPLE_CRATE_VERSIONS,
69 &format!("multiple versions for dependency `{}`: {}", name, versions),