]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/cargo_common_metadata.rs
Auto merge of #4885 - rust-lang:mut-key-types, r=flip1995
[rust.git] / clippy_lints / src / cargo_common_metadata.rs
1 //! lint on missing cargo common metadata
2
3 use std::path::PathBuf;
4
5 use crate::utils::span_lint;
6 use rustc::declare_lint_pass;
7 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
8 use rustc_session::declare_tool_lint;
9 use syntax::{ast::*, source_map::DUMMY_SP};
10
11 declare_clippy_lint! {
12     /// **What it does:** Checks to see if all common metadata is defined in
13     /// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata
14     ///
15     /// **Why is this bad?** It will be more difficult for users to discover the
16     /// purpose of the crate, and key information related to it.
17     ///
18     /// **Known problems:** None.
19     ///
20     /// **Example:**
21     /// ```toml
22     /// # This `Cargo.toml` is missing an authors field:
23     /// [package]
24     /// name = "clippy"
25     /// version = "0.0.212"
26     /// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
27     /// repository = "https://github.com/rust-lang/rust-clippy"
28     /// readme = "README.md"
29     /// license = "MIT OR Apache-2.0"
30     /// keywords = ["clippy", "lint", "plugin"]
31     /// categories = ["development-tools", "development-tools::cargo-plugins"]
32     /// ```
33     pub CARGO_COMMON_METADATA,
34     cargo,
35     "common metadata is defined in `Cargo.toml`"
36 }
37
38 fn warning(cx: &EarlyContext<'_>, message: &str) {
39     span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
40 }
41
42 fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, field: &str) {
43     let message = format!("package `{}` is missing `{}` metadata", package.name, field);
44     warning(cx, &message);
45 }
46
47 fn is_empty_str(value: &Option<String>) -> bool {
48     value.as_ref().map_or(true, String::is_empty)
49 }
50
51 fn is_empty_path(value: &Option<PathBuf>) -> bool {
52     value.as_ref().and_then(|x| x.to_str()).map_or(true, str::is_empty)
53 }
54
55 fn is_empty_vec(value: &[String]) -> bool {
56     // This works because empty iterators return true
57     value.iter().all(String::is_empty)
58 }
59
60 declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]);
61
62 impl EarlyLintPass for CargoCommonMetadata {
63     fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
64         let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
65             metadata
66         } else {
67             warning(cx, "could not read cargo metadata");
68             return;
69         };
70
71         for package in metadata.packages {
72             if is_empty_vec(&package.authors) {
73                 missing_warning(cx, &package, "package.authors");
74             }
75
76             if is_empty_str(&package.description) {
77                 missing_warning(cx, &package, "package.description");
78             }
79
80             if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
81                 missing_warning(cx, &package, "either package.license or package.license_file");
82             }
83
84             if is_empty_str(&package.repository) {
85                 missing_warning(cx, &package, "package.repository");
86             }
87
88             if is_empty_path(&package.readme) {
89                 missing_warning(cx, &package, "package.readme");
90             }
91
92             if is_empty_vec(&package.keywords) {
93                 missing_warning(cx, &package, "package.keywords");
94             }
95
96             if is_empty_vec(&package.categories) {
97                 missing_warning(cx, &package, "package.categories");
98             }
99         }
100     }
101 }