1 //! lint on missing cargo common metadata
3 use std::path::PathBuf;
5 use crate::utils::span_lint;
6 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
7 use rustc::{declare_lint_pass, declare_tool_lint};
8 use syntax::{ast::*, 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"]
34 pub CARGO_COMMON_METADATA,
36 "common metadata is defined in `Cargo.toml`"
39 fn warning(cx: &EarlyContext<'_>, message: &str) {
40 span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
43 fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, field: &str) {
44 let message = format!("package `{}` is missing `{}` metadata", package.name, field);
45 warning(cx, &message);
48 fn is_empty_str(value: &Option<String>) -> bool {
49 value.as_ref().map_or(true, String::is_empty)
52 fn is_empty_path(value: &Option<PathBuf>) -> bool {
53 value.as_ref().and_then(|x| x.to_str()).map_or(true, str::is_empty)
56 fn is_empty_vec(value: &[String]) -> bool {
57 // This works because empty iterators return true
58 value.iter().all(std::string::String::is_empty)
61 declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]);
63 impl EarlyLintPass for CargoCommonMetadata {
64 fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
65 let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
68 warning(cx, "could not read cargo metadata");
72 for package in metadata.packages {
73 if is_empty_vec(&package.authors) {
74 missing_warning(cx, &package, "package.authors");
77 if is_empty_str(&package.description) {
78 missing_warning(cx, &package, "package.description");
81 if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
82 missing_warning(cx, &package, "either package.license or package.license_file");
85 if is_empty_str(&package.repository) {
86 missing_warning(cx, &package, "package.repository");
89 if is_empty_path(&package.readme) {
90 missing_warning(cx, &package, "package.readme");
93 if is_empty_vec(&package.keywords) {
94 missing_warning(cx, &package, "package.keywords");
97 if is_empty_vec(&package.categories) {
98 missing_warning(cx, &package, "package.categories");