1 //! lint on missing cargo common metadata
3 use std::path::PathBuf;
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;
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
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.
19 /// **Known problems:** None.
23 /// # This `Cargo.toml` is missing an authors field:
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"]
35 /// Should include an authors field like:
38 /// # This `Cargo.toml` includes all common metadata
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"]
50 pub CARGO_COMMON_METADATA,
52 "common metadata is defined in `Cargo.toml`"
55 #[derive(Copy, Clone, Debug)]
56 pub struct CargoCommonMetadata {
60 impl CargoCommonMetadata {
61 pub fn new(ignore_publish: bool) -> Self {
62 Self { ignore_publish }
66 impl_lint_pass!(CargoCommonMetadata => [
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);
75 fn is_empty_str(value: &Option<String>) -> bool {
76 value.as_ref().map_or(true, String::is_empty)
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)
83 fn is_empty_vec(value: &[String]) -> bool {
84 // This works because empty iterators return true
85 value.iter().all(String::is_empty)
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) {
94 let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
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");
104 if is_empty_str(&package.description) {
105 missing_warning(cx, &package, "package.description");
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");
112 if is_empty_str(&package.repository) {
113 missing_warning(cx, &package, "package.repository");
116 if is_empty_path(&package.readme) {
117 missing_warning(cx, &package, "package.readme");
120 if is_empty_vec(&package.keywords) {
121 missing_warning(cx, &package, "package.keywords");
124 if is_empty_vec(&package.categories) {
125 missing_warning(cx, &package, "package.categories");