]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/wildcard_dependencies.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / wildcard_dependencies.rs
1 use crate::utils::span_lint;
2 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3 use rustc::{declare_tool_lint, lint_array};
4 use syntax::{ast::*, source_map::DUMMY_SP};
5
6 use cargo_metadata;
7 use if_chain::if_chain;
8 use semver;
9
10 declare_clippy_lint! {
11     /// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.
12     ///
13     /// **Why is this bad?** [As the edition guide says](https://rust-lang-nursery.github.io/edition-guide/rust-2018/cargo-and-crates-io/crates-io-disallows-wildcard-dependencies.html),
14     /// it is highly unlikely that you work with any possible version of your dependency,
15     /// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
16     ///
17     /// **Known problems:** None.
18     ///
19     /// **Example:**
20     ///
21     /// ```toml
22     /// [dependencies]
23     /// regex = "*"
24     /// ```
25     pub WILDCARD_DEPENDENCIES,
26     cargo,
27     "wildcard dependencies being used"
28 }
29
30 pub struct Pass;
31
32 impl LintPass for Pass {
33     fn get_lints(&self) -> LintArray {
34         lint_array!(WILDCARD_DEPENDENCIES)
35     }
36
37     fn name(&self) -> &'static str {
38         "WildcardDependencies"
39     }
40 }
41
42 impl EarlyLintPass for Pass {
43     fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
44         let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
45             metadata
46         } else {
47             span_lint(cx, WILDCARD_DEPENDENCIES, DUMMY_SP, "could not read cargo metadata");
48             return;
49         };
50
51         for dep in &metadata.packages[0].dependencies {
52             // VersionReq::any() does not work
53             if_chain! {
54                 if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
55                 if let Some(ref source) = dep.source;
56                 if !source.starts_with("git");
57                 if dep.req == wildcard_ver;
58                 then {
59                     span_lint(
60                         cx,
61                         WILDCARD_DEPENDENCIES,
62                         DUMMY_SP,
63                         &format!("wildcard dependency for `{}`", dep.name),
64                     );
65                 }
66             }
67         }
68     }
69 }