]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/wildcard_dependencies.rs
Replace remaining `krate.span` with `DUMMY_SP`
[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::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
11 use crate::rustc::{declare_tool_lint, lint_array};
12 use crate::syntax::{ast::*, source_map::DUMMY_SP};
13 use crate::utils::span_lint;
14
15 use cargo_metadata;
16 use semver;
17
18 /// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.
19 ///
20 /// **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),
21 /// it is highly unlikely that you work with any possible version of your dependency,
22 /// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
23 ///
24 /// **Known problems:** None.
25 ///
26 /// **Example:**
27 ///
28 /// ```toml
29 /// [dependencies]
30 /// regex = "*"
31 /// ```
32 declare_clippy_lint! {
33     pub WILDCARD_DEPENDENCIES,
34     cargo,
35     "wildcard dependencies being used"
36 }
37
38 pub struct Pass;
39
40 impl LintPass for Pass {
41     fn get_lints(&self) -> LintArray {
42         lint_array!(WILDCARD_DEPENDENCIES)
43     }
44 }
45
46 impl EarlyLintPass for Pass {
47     fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
48         let metadata = if let Ok(metadata) = cargo_metadata::metadata(None) {
49             metadata
50         } else {
51             span_lint(cx, WILDCARD_DEPENDENCIES, DUMMY_SP, "could not read cargo metadata");
52             return;
53         };
54
55         for dep in &metadata.packages[0].dependencies {
56             // VersionReq::any() does not work
57             if let Ok(wildcard_ver) = semver::VersionReq::parse("*") {
58                 if dep.req == wildcard_ver {
59                     span_lint(
60                         cx,
61                         WILDCARD_DEPENDENCIES,
62                         DUMMY_SP,
63                         &format!("wildcard dependency for `{}`", dep.name),
64                     );
65                 }
66             }
67         }
68     }
69 }