]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/cargo_common_metadata.rs
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[rust.git] / clippy_lints / src / cargo_common_metadata.rs
index 1a053de27d132d3d2225c5fa62553dad639bc06c..ec53e08f50cd3a1edeb4bdb56496f44cb473d618 100644 (file)
@@ -1,43 +1,35 @@
-// 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.
-
 //! lint on missing cargo common metadata
 
-use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
-use crate::rustc::{declare_tool_lint, lint_array};
-use crate::syntax::{ast::*, source_map::DUMMY_SP};
+use std::path::PathBuf;
+
 use crate::utils::span_lint;
+use rustc_lint::{EarlyContext, EarlyLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::source_map::DUMMY_SP;
+use syntax::ast::*;
 
-use cargo_metadata;
-
-/// **What it does:** Checks to see if all common metadata is defined in
-/// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata
-///
-/// **Why is this bad?** It will be more difficult for users to discover the
-/// purpose of the crate, and key information related to it.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```toml
-/// # This `Cargo.toml` is missing an authors field:
-/// [package]
-/// name = "clippy"
-/// version = "0.0.212"
-/// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
-/// repository = "https://github.com/rust-lang-nursery/rust-clippy"
-/// readme = "README.md"
-/// license = "MIT/Apache-2.0"
-/// keywords = ["clippy", "lint", "plugin"]
-/// categories = ["development-tools", "development-tools::cargo-plugins"]
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks to see if all common metadata is defined in
+    /// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata
+    ///
+    /// **Why is this bad?** It will be more difficult for users to discover the
+    /// purpose of the crate, and key information related to it.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```toml
+    /// # This `Cargo.toml` is missing an authors field:
+    /// [package]
+    /// name = "clippy"
+    /// version = "0.0.212"
+    /// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
+    /// repository = "https://github.com/rust-lang/rust-clippy"
+    /// readme = "README.md"
+    /// license = "MIT OR Apache-2.0"
+    /// keywords = ["clippy", "lint", "plugin"]
+    /// categories = ["development-tools", "development-tools::cargo-plugins"]
+    /// ```
     pub CARGO_COMMON_METADATA,
     cargo,
     "common metadata is defined in `Cargo.toml`"
@@ -53,29 +45,23 @@ fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, fie
 }
 
 fn is_empty_str(value: &Option<String>) -> bool {
-    match value {
-        None => true,
-        Some(value) if value.is_empty() => true,
-        _ => false
-    }
+    value.as_ref().map_or(true, String::is_empty)
+}
+
+fn is_empty_path(value: &Option<PathBuf>) -> bool {
+    value.as_ref().and_then(|x| x.to_str()).map_or(true, str::is_empty)
 }
 
 fn is_empty_vec(value: &[String]) -> bool {
     // This works because empty iterators return true
-    value.iter().all(|v| v.is_empty())
+    value.iter().all(String::is_empty)
 }
 
-pub struct Pass;
-
-impl LintPass for Pass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(CARGO_COMMON_METADATA)
-    }
-}
+declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]);
 
-impl EarlyLintPass for Pass {
+impl EarlyLintPass for CargoCommonMetadata {
     fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
-        let metadata = if let Ok(metadata) = cargo_metadata::metadata_deps(None, true) {
+        let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
             metadata
         } else {
             warning(cx, "could not read cargo metadata");
@@ -91,15 +77,15 @@ fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
                 missing_warning(cx, &package, "package.description");
             }
 
-            if is_empty_str(&package.license) {
-                missing_warning(cx, &package, "package.license");
+            if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
+                missing_warning(cx, &package, "either package.license or package.license_file");
             }
 
             if is_empty_str(&package.repository) {
                 missing_warning(cx, &package, "package.repository");
             }
 
-            if is_empty_str(&package.readme) {
+            if is_empty_path(&package.readme) {
                 missing_warning(cx, &package, "package.readme");
             }