]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/cargo_common_metadata.rs
Auto merge of #6601 - mdm:fix-unit-args-false-positive, r=camsteffen
[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::{run_lints, span_lint};
6 use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 use rustc_span::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     ///
34     /// Should include an authors field like:
35     ///
36     /// ```toml
37     /// # This `Cargo.toml` includes all common metadata
38     /// [package]
39     /// name = "clippy"
40     /// version = "0.0.212"
41     /// authors = ["Someone <someone@rust-lang.org>"]
42     /// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
43     /// repository = "https://github.com/rust-lang/rust-clippy"
44     /// readme = "README.md"
45     /// license = "MIT OR Apache-2.0"
46     /// keywords = ["clippy", "lint", "plugin"]
47     /// categories = ["development-tools", "development-tools::cargo-plugins"]
48     /// ```
49     pub CARGO_COMMON_METADATA,
50     cargo,
51     "common metadata is defined in `Cargo.toml`"
52 }
53
54 #[derive(Copy, Clone, Debug)]
55 pub struct CargoCommonMetadata {
56     ignore_publish: bool,
57 }
58
59 impl CargoCommonMetadata {
60     pub fn new(ignore_publish: bool) -> Self {
61         Self { ignore_publish }
62     }
63 }
64
65 impl_lint_pass!(CargoCommonMetadata => [
66     CARGO_COMMON_METADATA
67 ]);
68
69 fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
70     let message = format!("package `{}` is missing `{}` metadata", package.name, field);
71     span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
72 }
73
74 fn is_empty_str(value: &Option<String>) -> bool {
75     value.as_ref().map_or(true, String::is_empty)
76 }
77
78 fn is_empty_path(value: &Option<PathBuf>) -> bool {
79     value.as_ref().and_then(|x| x.to_str()).map_or(true, str::is_empty)
80 }
81
82 fn is_empty_vec(value: &[String]) -> bool {
83     // This works because empty iterators return true
84     value.iter().all(String::is_empty)
85 }
86
87 impl LateLintPass<'_> for CargoCommonMetadata {
88     fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
89         if !run_lints(cx, &[CARGO_COMMON_METADATA], CRATE_HIR_ID) {
90             return;
91         }
92
93         let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
94
95         for package in metadata.packages {
96             // only run the lint if publish is `None` (`publish = true` or skipped entirely)
97             // or if the vector isn't empty (`publish = ["something"]`)
98             if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
99                 if is_empty_vec(&package.authors) {
100                     missing_warning(cx, &package, "package.authors");
101                 }
102
103                 if is_empty_str(&package.description) {
104                     missing_warning(cx, &package, "package.description");
105                 }
106
107                 if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
108                     missing_warning(cx, &package, "either package.license or package.license_file");
109                 }
110
111                 if is_empty_str(&package.repository) {
112                     missing_warning(cx, &package, "package.repository");
113                 }
114
115                 if is_empty_path(&package.readme) {
116                     missing_warning(cx, &package, "package.readme");
117                 }
118
119                 if is_empty_vec(&package.keywords) {
120                     missing_warning(cx, &package, "package.keywords");
121                 }
122
123                 if is_empty_vec(&package.categories) {
124                     missing_warning(cx, &package, "package.categories");
125                 }
126             }
127         }
128     }
129 }