]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/wildcard_dependencies.rs
Auto merge of #87387 - the8472:slice-iter-advance_by, r=scottmcm
[rust.git] / src / tools / clippy / clippy_lints / src / wildcard_dependencies.rs
1 use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
2 use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
5 use rustc_span::source_map::DUMMY_SP;
6
7 use if_chain::if_chain;
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Checks for wildcard dependencies in the `Cargo.toml`.
12     ///
13     /// ### Why is this bad?
14     /// [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),
15     /// it is highly unlikely that you work with any possible version of your dependency,
16     /// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
17     ///
18     /// ### Example
19     /// ```toml
20     /// [dependencies]
21     /// regex = "*"
22     /// ```
23     pub WILDCARD_DEPENDENCIES,
24     cargo,
25     "wildcard dependencies being used"
26 }
27
28 declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
29
30 impl LateLintPass<'_> for WildcardDependencies {
31     fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
32         if is_lint_allowed(cx, WILDCARD_DEPENDENCIES, CRATE_HIR_ID) {
33             return;
34         }
35
36         let metadata = unwrap_cargo_metadata!(cx, WILDCARD_DEPENDENCIES, false);
37
38         for dep in &metadata.packages[0].dependencies {
39             // VersionReq::any() does not work
40             if_chain! {
41                 if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
42                 if let Some(ref source) = dep.source;
43                 if !source.starts_with("git");
44                 if dep.req == wildcard_ver;
45                 then {
46                     span_lint(
47                         cx,
48                         WILDCARD_DEPENDENCIES,
49                         DUMMY_SP,
50                         &format!("wildcard dependency for `{}`", dep.name),
51                     );
52                 }
53             }
54         }
55     }
56 }