]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/wildcard_dependencies.rs
Rollup merge of #85760 - ChrisDenton:path-doc-platform-specific, r=m-ou-se
[rust.git] / src / tools / clippy / clippy_lints / src / wildcard_dependencies.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::run_lints;
3 use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::source_map::DUMMY_SP;
7
8 use if_chain::if_chain;
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 LateLintPass<'_> for WildcardDependencies {
33     fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
34         if !run_lints(cx, &[WILDCARD_DEPENDENCIES], CRATE_HIR_ID) {
35             return;
36         }
37
38         let metadata = unwrap_cargo_metadata!(cx, WILDCARD_DEPENDENCIES, false);
39
40         for dep in &metadata.packages[0].dependencies {
41             // VersionReq::any() does not work
42             if_chain! {
43                 if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
44                 if let Some(ref source) = dep.source;
45                 if !source.starts_with("git");
46                 if dep.req == wildcard_ver;
47                 then {
48                     span_lint(
49                         cx,
50                         WILDCARD_DEPENDENCIES,
51                         DUMMY_SP,
52                         &format!("wildcard dependency for `{}`", dep.name),
53                     );
54                 }
55             }
56         }
57     }
58 }