]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/wildcard_dependencies.rs
Rollup merge of #82917 - cuviper:iter-zip, r=m-ou-se
[rust.git] / clippy_lints / src / wildcard_dependencies.rs
index 3045130231c99fde8800894d20a8c0a6aae9b3c7..60c3489a449b6796714a416120985ec2e8a7f33d 100644 (file)
@@ -1,69 +1,57 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
+use clippy_utils::diagnostics::span_lint;
+use clippy_utils::run_lints;
+use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::source_map::DUMMY_SP;
 
-use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
-use crate::rustc::{declare_tool_lint, lint_array};
-use crate::syntax::ast::*;
-use crate::utils::span_lint;
+use if_chain::if_chain;
 
-use cargo_metadata;
-use lazy_static::lazy_static;
-use semver;
-
-/// **What it does:** Checks to see if wildcard dependencies are being used.
-///
-/// **Why is this bad?** [As the edition guide sais](https://rust-lang-nursery.github.io/edition-guide/rust-2018/cargo-and-crates-io/crates-io-disallows-wildcard-dependencies.html),
-/// it is highly unlikely that you work with any possible version of your dependency,
-/// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```toml
-/// [dependencies]
-/// regex = "*"
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for wildcard dependencies in the `Cargo.toml`.
+    ///
+    /// **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),
+    /// it is highly unlikely that you work with any possible version of your dependency,
+    /// and wildcard dependencies would cause unnecessary breakage in the ecosystem.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    ///
+    /// ```toml
+    /// [dependencies]
+    /// regex = "*"
+    /// ```
     pub WILDCARD_DEPENDENCIES,
     cargo,
     "wildcard dependencies being used"
 }
 
-pub struct Pass;
-
-impl LintPass for Pass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(WILDCARD_DEPENDENCIES)
-    }
-}
+declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
 
-impl EarlyLintPass for Pass {
-    fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
-        let metadata = if let Ok(metadata) = cargo_metadata::metadata(None) {
-            metadata
-        } else {
-            span_lint(cx, WILDCARD_DEPENDENCIES, krate.span, "could not read cargo metadata");
+impl LateLintPass<'_> for WildcardDependencies {
+    fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
+        if !run_lints(cx, &[WILDCARD_DEPENDENCIES], CRATE_HIR_ID) {
             return;
-        };
-
-        lazy_static! {
-            static ref WILDCARD_VERSION_REQ: semver::VersionReq = semver::VersionReq::parse("*").unwrap();
         }
 
+        let metadata = unwrap_cargo_metadata!(cx, WILDCARD_DEPENDENCIES, false);
+
         for dep in &metadata.packages[0].dependencies {
-            if dep.req == *WILDCARD_VERSION_REQ {
-                span_lint(
-                    cx,
-                    WILDCARD_DEPENDENCIES,
-                    krate.span,
-                    &format!("wildcard dependency for `{}`", dep.name),
-                );
+            // VersionReq::any() does not work
+            if_chain! {
+                if let Ok(wildcard_ver) = semver::VersionReq::parse("*");
+                if let Some(ref source) = dep.source;
+                if !source.starts_with("git");
+                if dep.req == wildcard_ver;
+                then {
+                    span_lint(
+                        cx,
+                        WILDCARD_DEPENDENCIES,
+                        DUMMY_SP,
+                        &format!("wildcard dependency for `{}`", dep.name),
+                    );
+                }
             }
         }
     }