]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/wildcard_dependencies.rs
New upstream version 1.55.0+dfsg1
[rustc.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:** 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 pub WILDCARD_DEPENDENCIES,
25 cargo,
26 "wildcard dependencies being used"
27 }
28
29 declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
30
31 impl LateLintPass<'_> for WildcardDependencies {
32 fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
33 if is_lint_allowed(cx, WILDCARD_DEPENDENCIES, CRATE_HIR_ID) {
34 return;
35 }
36
37 let metadata = unwrap_cargo_metadata!(cx, WILDCARD_DEPENDENCIES, false);
38
39 for dep in &metadata.packages[0].dependencies {
40 // VersionReq::any() does not work
41 if_chain! {
42 if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
43 if let Some(ref source) = dep.source;
44 if !source.starts_with("git");
45 if dep.req == wildcard_ver;
46 then {
47 span_lint(
48 cx,
49 WILDCARD_DEPENDENCIES,
50 DUMMY_SP,
51 &format!("wildcard dependency for `{}`", dep.name),
52 );
53 }
54 }
55 }
56 }
57 }