]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/wildcard_dependencies.rs
Merge remote-tracking branch 'origin/rust-1.31.0' into HEAD
[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_lint_pass, declare_tool_lint};
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 declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
31
32 impl EarlyLintPass for WildcardDependencies {
33     fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
34         let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
35             metadata
36         } else {
37             span_lint(cx, WILDCARD_DEPENDENCIES, DUMMY_SP, "could not read cargo metadata");
38             return;
39         };
40
41         for dep in &metadata.packages[0].dependencies {
42             // VersionReq::any() does not work
43             if_chain! {
44                 if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
45                 if let Some(ref source) = dep.source;
46                 if !source.starts_with("git");
47                 if dep.req == wildcard_ver;
48                 then {
49                     span_lint(
50                         cx,
51                         WILDCARD_DEPENDENCIES,
52                         DUMMY_SP,
53                         &format!("wildcard dependency for `{}`", dep.name),
54                     );
55                 }
56             }
57         }
58     }
59 }