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