]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/cargo_common_metadata.rs
Rollup merge of #91562 - dtolnay:asyncspace, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / clippy_lints / src / cargo_common_metadata.rs
1 //! lint on missing cargo common metadata
2
3 use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
4 use rustc_hir::hir_id::CRATE_HIR_ID;
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_tool_lint, impl_lint_pass};
7 use rustc_span::source_map::DUMMY_SP;
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Checks to see if all common metadata is defined in
12     /// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata
13     ///
14     /// ### Why is this bad?
15     /// It will be more difficult for users to discover the
16     /// purpose of the crate, and key information related to it.
17     ///
18     /// ### Example
19     /// ```toml
20     /// # This `Cargo.toml` is missing a description field:
21     /// [package]
22     /// name = "clippy"
23     /// version = "0.0.212"
24     /// repository = "https://github.com/rust-lang/rust-clippy"
25     /// readme = "README.md"
26     /// license = "MIT OR Apache-2.0"
27     /// keywords = ["clippy", "lint", "plugin"]
28     /// categories = ["development-tools", "development-tools::cargo-plugins"]
29     /// ```
30     ///
31     /// Should include a description field like:
32     ///
33     /// ```toml
34     /// # This `Cargo.toml` includes all common metadata
35     /// [package]
36     /// name = "clippy"
37     /// version = "0.0.212"
38     /// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
39     /// repository = "https://github.com/rust-lang/rust-clippy"
40     /// readme = "README.md"
41     /// license = "MIT OR Apache-2.0"
42     /// keywords = ["clippy", "lint", "plugin"]
43     /// categories = ["development-tools", "development-tools::cargo-plugins"]
44     /// ```
45     #[clippy::version = "1.32.0"]
46     pub CARGO_COMMON_METADATA,
47     cargo,
48     "common metadata is defined in `Cargo.toml`"
49 }
50
51 #[derive(Copy, Clone, Debug)]
52 pub struct CargoCommonMetadata {
53     ignore_publish: bool,
54 }
55
56 impl CargoCommonMetadata {
57     pub fn new(ignore_publish: bool) -> Self {
58         Self { ignore_publish }
59     }
60 }
61
62 impl_lint_pass!(CargoCommonMetadata => [
63     CARGO_COMMON_METADATA
64 ]);
65
66 fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
67     let message = format!("package `{}` is missing `{}` metadata", package.name, field);
68     span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
69 }
70
71 fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: &Option<T>) -> bool {
72     value.as_ref().map_or(true, |s| s.as_ref().is_empty())
73 }
74
75 fn is_empty_vec(value: &[String]) -> bool {
76     // This works because empty iterators return true
77     value.iter().all(String::is_empty)
78 }
79
80 impl LateLintPass<'_> for CargoCommonMetadata {
81     fn check_crate(&mut self, cx: &LateContext<'_>) {
82         if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) {
83             return;
84         }
85
86         let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
87
88         for package in metadata.packages {
89             // only run the lint if publish is `None` (`publish = true` or skipped entirely)
90             // or if the vector isn't empty (`publish = ["something"]`)
91             if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
92                 if is_empty_str(&package.description) {
93                     missing_warning(cx, &package, "package.description");
94                 }
95
96                 if is_empty_str(&package.license) && is_empty_str(&package.license_file) {
97                     missing_warning(cx, &package, "either package.license or package.license_file");
98                 }
99
100                 if is_empty_str(&package.repository) {
101                     missing_warning(cx, &package, "package.repository");
102                 }
103
104                 if is_empty_str(&package.readme) {
105                     missing_warning(cx, &package, "package.readme");
106                 }
107
108                 if is_empty_vec(&package.keywords) {
109                     missing_warning(cx, &package, "package.keywords");
110                 }
111
112                 if is_empty_vec(&package.categories) {
113                     missing_warning(cx, &package, "package.categories");
114                 }
115             }
116         }
117     }
118 }