1 //! lint on missing cargo common metadata
3 use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
4 use rustc_hir::hir_id::CRATE_HIR_ID;
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_tool_lint, impl_lint_pass};
7 use rustc_span::source_map::DUMMY_SP;
11 /// Checks to see if all common metadata is defined in
12 /// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata
14 /// ### Why is this bad?
15 /// It will be more difficult for users to discover the
16 /// purpose of the crate, and key information related to it.
20 /// # This `Cargo.toml` is missing a description field:
23 /// version = "0.0.212"
24 /// repository = "https://github.com/rust-lang/rust-clippy"
25 /// readme = "README.md"
26 /// license = "MIT OR Apache-2.0"
27 /// keywords = ["clippy", "lint", "plugin"]
28 /// categories = ["development-tools", "development-tools::cargo-plugins"]
31 /// Should include a description field like:
34 /// # This `Cargo.toml` includes all common metadata
37 /// version = "0.0.212"
38 /// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
39 /// repository = "https://github.com/rust-lang/rust-clippy"
40 /// readme = "README.md"
41 /// license = "MIT OR Apache-2.0"
42 /// keywords = ["clippy", "lint", "plugin"]
43 /// categories = ["development-tools", "development-tools::cargo-plugins"]
45 #[clippy::version = "1.32.0"]
46 pub CARGO_COMMON_METADATA,
48 "common metadata is defined in `Cargo.toml`"
51 #[derive(Copy, Clone, Debug)]
52 pub struct CargoCommonMetadata {
56 impl CargoCommonMetadata {
57 pub fn new(ignore_publish: bool) -> Self {
58 Self { ignore_publish }
62 impl_lint_pass!(CargoCommonMetadata => [
66 fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
67 let message = format!("package `{}` is missing `{}` metadata", package.name, field);
68 span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
71 fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: &Option<T>) -> bool {
72 value.as_ref().map_or(true, |s| s.as_ref().is_empty())
75 fn is_empty_vec(value: &[String]) -> bool {
76 // This works because empty iterators return true
77 value.iter().all(String::is_empty)
80 impl LateLintPass<'_> for CargoCommonMetadata {
81 fn check_crate(&mut self, cx: &LateContext<'_>) {
82 if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) {
86 let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
88 for package in metadata.packages {
89 // only run the lint if publish is `None` (`publish = true` or skipped entirely)
90 // or if the vector isn't empty (`publish = ["something"]`)
91 if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
92 if is_empty_str(&package.description) {
93 missing_warning(cx, &package, "package.description");
96 if is_empty_str(&package.license) && is_empty_str(&package.license_file) {
97 missing_warning(cx, &package, "either package.license or package.license_file");
100 if is_empty_str(&package.repository) {
101 missing_warning(cx, &package, "package.repository");
104 if is_empty_str(&package.readme) {
105 missing_warning(cx, &package, "package.readme");
108 if is_empty_vec(&package.keywords) {
109 missing_warning(cx, &package, "package.keywords");
112 if is_empty_vec(&package.categories) {
113 missing_warning(cx, &package, "package.categories");