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