1 //! lint on missing cargo common metadata
3 use std::path::PathBuf;
5 use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
6 use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 use rustc_span::source_map::DUMMY_SP;
11 declare_clippy_lint! {
13 /// 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?
17 /// It will be more difficult for users to discover the
18 /// purpose of the crate, and key information related to it.
22 /// # This `Cargo.toml` is missing a description field:
25 /// version = "0.0.212"
26 /// repository = "https://github.com/rust-lang/rust-clippy"
27 /// readme = "README.md"
28 /// license = "MIT OR Apache-2.0"
29 /// keywords = ["clippy", "lint", "plugin"]
30 /// categories = ["development-tools", "development-tools::cargo-plugins"]
33 /// Should include a description field like:
36 /// # This `Cargo.toml` includes all common metadata
39 /// version = "0.0.212"
40 /// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
41 /// repository = "https://github.com/rust-lang/rust-clippy"
42 /// readme = "README.md"
43 /// license = "MIT OR Apache-2.0"
44 /// keywords = ["clippy", "lint", "plugin"]
45 /// categories = ["development-tools", "development-tools::cargo-plugins"]
47 pub CARGO_COMMON_METADATA,
49 "common metadata is defined in `Cargo.toml`"
52 #[derive(Copy, Clone, Debug)]
53 pub struct CargoCommonMetadata {
57 impl CargoCommonMetadata {
58 pub fn new(ignore_publish: bool) -> Self {
59 Self { ignore_publish }
63 impl_lint_pass!(CargoCommonMetadata => [
67 fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
68 let message = format!("package `{}` is missing `{}` metadata", package.name, field);
69 span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
72 fn is_empty_str(value: &Option<String>) -> bool {
73 value.as_ref().map_or(true, String::is_empty)
76 fn is_empty_path(value: &Option<PathBuf>) -> bool {
77 value.as_ref().and_then(|x| x.to_str()).map_or(true, str::is_empty)
80 fn is_empty_vec(value: &[String]) -> bool {
81 // This works because empty iterators return true
82 value.iter().all(String::is_empty)
85 impl LateLintPass<'_> for CargoCommonMetadata {
86 fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
87 if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) {
91 let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
93 for package in metadata.packages {
94 // only run the lint if publish is `None` (`publish = true` or skipped entirely)
95 // or if the vector isn't empty (`publish = ["something"]`)
96 if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
97 if is_empty_str(&package.description) {
98 missing_warning(cx, &package, "package.description");
101 if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
102 missing_warning(cx, &package, "either package.license or package.license_file");
105 if is_empty_str(&package.repository) {
106 missing_warning(cx, &package, "package.repository");
109 if is_empty_path(&package.readme) {
110 missing_warning(cx, &package, "package.readme");
113 if is_empty_vec(&package.keywords) {
114 missing_warning(cx, &package, "package.keywords");
117 if is_empty_vec(&package.categories) {
118 missing_warning(cx, &package, "package.categories");