1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! lint on missing cargo common metadata
12 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::syntax::{ast::*, source_map::DUMMY_SP};
15 use crate::utils::span_lint;
19 /// **What it does:** Checks to see if all common metadata is defined in
20 /// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata
22 /// **Why is this bad?** It will be more difficult for users to discover the
23 /// purpose of the crate, and key information related to it.
25 /// **Known problems:** None.
29 /// # This `Cargo.toml` is missing an authors field:
32 /// version = "0.0.212"
33 /// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
34 /// repository = "https://github.com/rust-lang/rust-clippy"
35 /// readme = "README.md"
36 /// license = "MIT/Apache-2.0"
37 /// keywords = ["clippy", "lint", "plugin"]
38 /// categories = ["development-tools", "development-tools::cargo-plugins"]
40 declare_clippy_lint! {
41 pub CARGO_COMMON_METADATA,
43 "common metadata is defined in `Cargo.toml`"
46 fn warning(cx: &EarlyContext<'_>, message: &str) {
47 span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
50 fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, field: &str) {
51 let message = format!("package `{}` is missing `{}` metadata", package.name, field);
52 warning(cx, &message);
55 fn is_empty_str(value: &Option<String>) -> bool {
58 Some(value) if value.is_empty() => true,
63 fn is_empty_vec(value: &[String]) -> bool {
64 // This works because empty iterators return true
65 value.iter().all(|v| v.is_empty())
70 impl LintPass for Pass {
71 fn get_lints(&self) -> LintArray {
72 lint_array!(CARGO_COMMON_METADATA)
76 impl EarlyLintPass for Pass {
77 fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
78 let metadata = if let Ok(metadata) = cargo_metadata::metadata_deps(None, true) {
81 warning(cx, "could not read cargo metadata");
85 for package in metadata.packages {
86 if is_empty_vec(&package.authors) {
87 missing_warning(cx, &package, "package.authors");
90 if is_empty_str(&package.description) {
91 missing_warning(cx, &package, "package.description");
94 if is_empty_str(&package.license) {
95 missing_warning(cx, &package, "package.license");
98 if is_empty_str(&package.repository) {
99 missing_warning(cx, &package, "package.repository");
102 if is_empty_str(&package.readme) {
103 missing_warning(cx, &package, "package.readme");
106 if is_empty_vec(&package.keywords) {
107 missing_warning(cx, &package, "package.keywords");
110 if is_empty_vec(&package.categories) {
111 missing_warning(cx, &package, "package.categories");