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 pub CARGO_COMMON_METADATA,
47 "common metadata is defined in `Cargo.toml`"
50 #[derive(Copy, Clone, Debug)]
51 pub struct CargoCommonMetadata {
55 impl CargoCommonMetadata {
56 pub fn new(ignore_publish: bool) -> Self {
57 Self { ignore_publish }
61 impl_lint_pass!(CargoCommonMetadata => [
65 fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
66 let message = format!("package `{}` is missing `{}` metadata", package.name, field);
67 span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
70 fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: &Option<T>) -> bool {
71 value.as_ref().map_or(true, |s| s.as_ref().is_empty())
74 fn is_empty_vec(value: &[String]) -> bool {
75 // This works because empty iterators return true
76 value.iter().all(String::is_empty)
79 impl LateLintPass<'_> for CargoCommonMetadata {
80 fn check_crate(&mut self, cx: &LateContext<'_>) {
81 if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) {
85 let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
87 for package in metadata.packages {
88 // only run the lint if publish is `None` (`publish = true` or skipped entirely)
89 // or if the vector isn't empty (`publish = ["something"]`)
90 if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
91 if is_empty_str(&package.description) {
92 missing_warning(cx, &package, "package.description");
95 if is_empty_str(&package.license) && is_empty_str(&package.license_file) {
96 missing_warning(cx, &package, "either package.license or package.license_file");
99 if is_empty_str(&package.repository) {
100 missing_warning(cx, &package, "package.repository");
103 if is_empty_str(&package.readme) {
104 missing_warning(cx, &package, "package.readme");
107 if is_empty_vec(&package.keywords) {
108 missing_warning(cx, &package, "package.keywords");
111 if is_empty_vec(&package.categories) {
112 missing_warning(cx, &package, "package.categories");