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 a description field:
26 /// version = "0.0.212"
27 /// repository = "https://github.com/rust-lang/rust-clippy"
28 /// readme = "README.md"
29 /// license = "MIT OR Apache-2.0"
30 /// keywords = ["clippy", "lint", "plugin"]
31 /// categories = ["development-tools", "development-tools::cargo-plugins"]
34 /// Should include a description field like:
37 /// # This `Cargo.toml` includes all common metadata
40 /// version = "0.0.212"
41 /// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
42 /// repository = "https://github.com/rust-lang/rust-clippy"
43 /// readme = "README.md"
44 /// license = "MIT OR Apache-2.0"
45 /// keywords = ["clippy", "lint", "plugin"]
46 /// categories = ["development-tools", "development-tools::cargo-plugins"]
48 pub CARGO_COMMON_METADATA,
50 "common metadata is defined in `Cargo.toml`"
53 #[derive(Copy, Clone, Debug)]
54 pub struct CargoCommonMetadata {
58 impl CargoCommonMetadata {
59 pub fn new(ignore_publish: bool) -> Self {
60 Self { ignore_publish }
64 impl_lint_pass!(CargoCommonMetadata => [
68 fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
69 let message = format!("package `{}` is missing `{}` metadata", package.name, field);
70 span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
73 fn is_empty_str(value: &Option<String>) -> bool {
74 value.as_ref().map_or(true, String::is_empty)
77 fn is_empty_path(value: &Option<PathBuf>) -> bool {
78 value.as_ref().and_then(|x| x.to_str()).map_or(true, str::is_empty)
81 fn is_empty_vec(value: &[String]) -> bool {
82 // This works because empty iterators return true
83 value.iter().all(String::is_empty)
86 impl LateLintPass<'_> for CargoCommonMetadata {
87 fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
88 if !run_lints(cx, &[CARGO_COMMON_METADATA], CRATE_HIR_ID) {
92 let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
94 for package in metadata.packages {
95 // only run the lint if publish is `None` (`publish = true` or skipped entirely)
96 // or if the vector isn't empty (`publish = ["something"]`)
97 if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
98 if is_empty_str(&package.description) {
99 missing_warning(cx, &package, "package.description");
102 if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
103 missing_warning(cx, &package, "either package.license or package.license_file");
106 if is_empty_str(&package.repository) {
107 missing_warning(cx, &package, "package.repository");
110 if is_empty_path(&package.readme) {
111 missing_warning(cx, &package, "package.readme");
114 if is_empty_vec(&package.keywords) {
115 missing_warning(cx, &package, "package.keywords");
118 if is_empty_vec(&package.categories) {
119 missing_warning(cx, &package, "package.categories");