]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/cargo_common_metadata.rs
Merge commit '7ea7cd165ad6705603852771bf82cc2fd6560db5' into clippyup2
[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_lint_pass, declare_tool_lint};
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     /// authors = ["Someone <someone@rust-lang.org>"]
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     pub CARGO_COMMON_METADATA,
35     cargo,
36     "common metadata is defined in `Cargo.toml`"
37 }
38
39 fn warning(cx: &LateContext<'_, '_>, message: &str) {
40     span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
41 }
42
43 fn missing_warning(cx: &LateContext<'_, '_>, package: &cargo_metadata::Package, field: &str) {
44     let message = format!("package `{}` is missing `{}` metadata", package.name, field);
45     warning(cx, &message);
46 }
47
48 fn is_empty_str(value: &Option<String>) -> bool {
49     value.as_ref().map_or(true, String::is_empty)
50 }
51
52 fn is_empty_path(value: &Option<PathBuf>) -> bool {
53     value.as_ref().and_then(|x| x.to_str()).map_or(true, str::is_empty)
54 }
55
56 fn is_empty_vec(value: &[String]) -> bool {
57     // This works because empty iterators return true
58     value.iter().all(String::is_empty)
59 }
60
61 declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]);
62
63 impl LateLintPass<'_, '_> for CargoCommonMetadata {
64     fn check_crate(&mut self, cx: &LateContext<'_, '_>, _: &Crate<'_>) {
65         if !run_lints(cx, &[CARGO_COMMON_METADATA], CRATE_HIR_ID) {
66             return;
67         }
68
69         let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
70             metadata
71         } else {
72             warning(cx, "could not read cargo metadata");
73             return;
74         };
75
76         for package in metadata.packages {
77             if is_empty_vec(&package.authors) {
78                 missing_warning(cx, &package, "package.authors");
79             }
80
81             if is_empty_str(&package.description) {
82                 missing_warning(cx, &package, "package.description");
83             }
84
85             if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
86                 missing_warning(cx, &package, "either package.license or package.license_file");
87             }
88
89             if is_empty_str(&package.repository) {
90                 missing_warning(cx, &package, "package.repository");
91             }
92
93             if is_empty_path(&package.readme) {
94                 missing_warning(cx, &package, "package.readme");
95             }
96
97             if is_empty_vec(&package.keywords) {
98                 missing_warning(cx, &package, "package.keywords");
99             }
100
101             if is_empty_vec(&package.categories) {
102                 missing_warning(cx, &package, "package.categories");
103             }
104         }
105     }
106 }