]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/wildcard_dependencies.rs
Auto merge of #3645 - phansch:remove_copyright_headers, r=oli-obk
[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 /// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.
11 ///
12 /// **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),
13 /// it is highly unlikely that you work with any possible version of your dependency,
14 /// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
15 ///
16 /// **Known problems:** None.
17 ///
18 /// **Example:**
19 ///
20 /// ```toml
21 /// [dependencies]
22 /// regex = "*"
23 /// ```
24 declare_clippy_lint! {
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
38 impl EarlyLintPass for Pass {
39     fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
40         let metadata = if let Ok(metadata) = cargo_metadata::metadata(None) {
41             metadata
42         } else {
43             span_lint(cx, WILDCARD_DEPENDENCIES, DUMMY_SP, "could not read cargo metadata");
44             return;
45         };
46
47         for dep in &metadata.packages[0].dependencies {
48             // VersionReq::any() does not work
49             if_chain! {
50                 if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
51                 if let Some(ref source) = dep.source;
52                 if !source.starts_with("git");
53                 if dep.req == wildcard_ver;
54                 then {
55                     span_lint(
56                         cx,
57                         WILDCARD_DEPENDENCIES,
58                         DUMMY_SP,
59                         &format!("wildcard dependency for `{}`", dep.name),
60                     );
61                 }
62             }
63         }
64     }
65 }