]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/cargo_common_metadata.rs
Rollup merge of #89839 - jkugelman:must-use-mem-ptr-functions, r=joshtriplett
[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     pub CARGO_COMMON_METADATA,
46     cargo,
47     "common metadata is defined in `Cargo.toml`"
48 }
49
50 #[derive(Copy, Clone, Debug)]
51 pub struct CargoCommonMetadata {
52     ignore_publish: bool,
53 }
54
55 impl CargoCommonMetadata {
56     pub fn new(ignore_publish: bool) -> Self {
57         Self { ignore_publish }
58     }
59 }
60
61 impl_lint_pass!(CargoCommonMetadata => [
62     CARGO_COMMON_METADATA
63 ]);
64
65 fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
66     let message = format!("package `{}` is missing `{}` metadata", package.name, field);
67     span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
68 }
69
70 fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: &Option<T>) -> bool {
71     value.as_ref().map_or(true, |s| s.as_ref().is_empty())
72 }
73
74 fn is_empty_vec(value: &[String]) -> bool {
75     // This works because empty iterators return true
76     value.iter().all(String::is_empty)
77 }
78
79 impl LateLintPass<'_> for CargoCommonMetadata {
80     fn check_crate(&mut self, cx: &LateContext<'_>) {
81         if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) {
82             return;
83         }
84
85         let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
86
87         for package in metadata.packages {
88             // only run the lint if publish is `None` (`publish = true` or skipped entirely)
89             // or if the vector isn't empty (`publish = ["something"]`)
90             if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
91                 if is_empty_str(&package.description) {
92                     missing_warning(cx, &package, "package.description");
93                 }
94
95                 if is_empty_str(&package.license) && is_empty_str(&package.license_file) {
96                     missing_warning(cx, &package, "either package.license or package.license_file");
97                 }
98
99                 if is_empty_str(&package.repository) {
100                     missing_warning(cx, &package, "package.repository");
101                 }
102
103                 if is_empty_str(&package.readme) {
104                     missing_warning(cx, &package, "package.readme");
105                 }
106
107                 if is_empty_vec(&package.keywords) {
108                     missing_warning(cx, &package, "package.keywords");
109                 }
110
111                 if is_empty_vec(&package.categories) {
112                     missing_warning(cx, &package, "package.categories");
113                 }
114             }
115         }
116     }
117 }