]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/cargo_common_metadata.rs
Merge pull request #3336 from HMPerson1/clone_on_copy_deref
[rust.git] / clippy_lints / src / cargo_common_metadata.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 //! lint on missing cargo common metadata
11
12 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::syntax::{ast::*, source_map::DUMMY_SP};
15 use crate::utils::span_lint;
16
17 use cargo_metadata;
18
19 /// **What it does:** Checks to see if all common metadata is defined in
20 /// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata
21 ///
22 /// **Why is this bad?** It will be more difficult for users to discover the
23 /// purpose of the crate, and key information related to it.
24 ///
25 /// **Known problems:** None.
26 ///
27 /// **Example:**
28 /// ```toml
29 /// # This `Cargo.toml` is missing an authors field:
30 /// [package]
31 /// name = "clippy"
32 /// version = "0.0.212"
33 /// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
34 /// repository = "https://github.com/rust-lang/rust-clippy"
35 /// readme = "README.md"
36 /// license = "MIT/Apache-2.0"
37 /// keywords = ["clippy", "lint", "plugin"]
38 /// categories = ["development-tools", "development-tools::cargo-plugins"]
39 /// ```
40 declare_clippy_lint! {
41     pub CARGO_COMMON_METADATA,
42     cargo,
43     "common metadata is defined in `Cargo.toml`"
44 }
45
46 fn warning(cx: &EarlyContext<'_>, message: &str) {
47     span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
48 }
49
50 fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, field: &str) {
51     let message = format!("package `{}` is missing `{}` metadata", package.name, field);
52     warning(cx, &message);
53 }
54
55 fn is_empty_str(value: &Option<String>) -> bool {
56     match value {
57         None => true,
58         Some(value) if value.is_empty() => true,
59         _ => false,
60     }
61 }
62
63 fn is_empty_vec(value: &[String]) -> bool {
64     // This works because empty iterators return true
65     value.iter().all(|v| v.is_empty())
66 }
67
68 pub struct Pass;
69
70 impl LintPass for Pass {
71     fn get_lints(&self) -> LintArray {
72         lint_array!(CARGO_COMMON_METADATA)
73     }
74 }
75
76 impl EarlyLintPass for Pass {
77     fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
78         let metadata = if let Ok(metadata) = cargo_metadata::metadata_deps(None, true) {
79             metadata
80         } else {
81             warning(cx, "could not read cargo metadata");
82             return;
83         };
84
85         for package in metadata.packages {
86             if is_empty_vec(&package.authors) {
87                 missing_warning(cx, &package, "package.authors");
88             }
89
90             if is_empty_str(&package.description) {
91                 missing_warning(cx, &package, "package.description");
92             }
93
94             if is_empty_str(&package.license) {
95                 missing_warning(cx, &package, "package.license");
96             }
97
98             if is_empty_str(&package.repository) {
99                 missing_warning(cx, &package, "package.repository");
100             }
101
102             if is_empty_str(&package.readme) {
103                 missing_warning(cx, &package, "package.readme");
104             }
105
106             if is_empty_vec(&package.keywords) {
107                 missing_warning(cx, &package, "package.keywords");
108             }
109
110             if is_empty_vec(&package.categories) {
111                 missing_warning(cx, &package, "package.categories");
112             }
113         }
114     }
115 }