1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 //! lint on multiple versions of a crate being used
13 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::syntax::ast::*;
16 use crate::utils::span_lint;
19 use itertools::Itertools;
21 /// **What it does:** Checks to see if multiple versions of a crate are being
24 /// **Why is this bad?** This bloats the size of targets, and can lead to
25 /// confusing error messages when structs or traits are used interchangeably
26 /// between different versions of a crate.
28 /// **Known problems:** Because this can be caused purely by the dependencies
29 /// themselves, it's not always possible to fix this issue.
33 /// # This will pull in both winapi v0.3.4 and v0.2.8, triggering a warning.
36 /// ansi_term = "0.11.0"
38 declare_clippy_lint! {
39 pub MULTIPLE_CRATE_VERSIONS,
41 "multiple versions of the same crate being used"
46 impl LintPass for Pass {
47 fn get_lints(&self) -> LintArray {
48 lint_array!(MULTIPLE_CRATE_VERSIONS)
52 impl EarlyLintPass for Pass {
53 fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
54 let metadata = if let Ok(metadata) = cargo_metadata::metadata_deps(None, true) {
59 MULTIPLE_CRATE_VERSIONS,
61 "could not read cargo metadata"
67 let mut packages = metadata.packages;
68 packages.sort_by(|a, b| a.name.cmp(&b.name));
70 for (name, group) in &packages.into_iter().group_by(|p| p.name.clone()) {
71 let group: Vec<cargo_metadata::Package> = group.collect();
74 let versions = group.into_iter().map(|p| p.version).join(", ");
78 MULTIPLE_CRATE_VERSIONS,
80 &format!("multiple versions for dependency `{}`: {}", name, versions),