]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/wildcard_dependencies.rs
Auto merge of #3598 - xfix:apply-cargo-fix-edition-idioms, r=phansch
[rust.git] / clippy_lints / src / wildcard_dependencies.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10 use crate::utils::span_lint;
11 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
12 use rustc::{declare_tool_lint, lint_array};
13 use syntax::{ast::*, source_map::DUMMY_SP};
14
15 use cargo_metadata;
16 use if_chain::if_chain;
17 use semver;
18
19 /// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.
20 ///
21 /// **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),
22 /// it is highly unlikely that you work with any possible version of your dependency,
23 /// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
24 ///
25 /// **Known problems:** None.
26 ///
27 /// **Example:**
28 ///
29 /// ```toml
30 /// [dependencies]
31 /// regex = "*"
32 /// ```
33 declare_clippy_lint! {
34     pub WILDCARD_DEPENDENCIES,
35     cargo,
36     "wildcard dependencies being used"
37 }
38
39 pub struct Pass;
40
41 impl LintPass for Pass {
42     fn get_lints(&self) -> LintArray {
43         lint_array!(WILDCARD_DEPENDENCIES)
44     }
45 }
46
47 impl EarlyLintPass for Pass {
48     fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
49         let metadata = if let Ok(metadata) = cargo_metadata::metadata(None) {
50             metadata
51         } else {
52             span_lint(cx, WILDCARD_DEPENDENCIES, DUMMY_SP, "could not read cargo metadata");
53             return;
54         };
55
56         for dep in &metadata.packages[0].dependencies {
57             // VersionReq::any() does not work
58             if_chain! {
59                 if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
60                 if let Some(ref source) = dep.source;
61                 if !source.starts_with("git");
62                 if dep.req == wildcard_ver;
63                 then {
64                     span_lint(
65                         cx,
66                         WILDCARD_DEPENDENCIES,
67                         DUMMY_SP,
68                         &format!("wildcard dependency for `{}`", dep.name),
69                     );
70                 }
71             }
72         }
73     }
74 }