2018-11-13 13:43:30 +00:00
|
|
|
//! lint on missing cargo common metadata
|
|
|
|
|
2019-09-07 08:00:02 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-04-21 20:53:18 +00:00
|
|
|
use crate::utils::{run_lints, span_lint};
|
|
|
|
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::DUMMY_SP;
|
2018-11-13 13:43:30 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks to see if all common metadata is defined in
|
|
|
|
/// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It will be more difficult for users to discover the
|
|
|
|
/// purpose of the crate, and key information related to it.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```toml
|
|
|
|
/// # This `Cargo.toml` is missing an authors field:
|
|
|
|
/// [package]
|
|
|
|
/// name = "clippy"
|
|
|
|
/// version = "0.0.212"
|
2020-11-04 22:39:52 +00:00
|
|
|
/// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
|
|
|
|
/// repository = "https://github.com/rust-lang/rust-clippy"
|
|
|
|
/// readme = "README.md"
|
|
|
|
/// license = "MIT OR Apache-2.0"
|
|
|
|
/// keywords = ["clippy", "lint", "plugin"]
|
|
|
|
/// categories = ["development-tools", "development-tools::cargo-plugins"]
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Should include an authors field like:
|
|
|
|
///
|
|
|
|
/// ```toml
|
|
|
|
/// # This `Cargo.toml` includes all common metadata
|
|
|
|
/// [package]
|
|
|
|
/// name = "clippy"
|
|
|
|
/// version = "0.0.212"
|
2020-05-28 13:45:24 +00:00
|
|
|
/// authors = ["Someone <someone@rust-lang.org>"]
|
2019-03-05 16:50:33 +00:00
|
|
|
/// description = "A bunch of helpful lints to avoid common pitfalls in Rust"
|
|
|
|
/// repository = "https://github.com/rust-lang/rust-clippy"
|
|
|
|
/// readme = "README.md"
|
2019-10-04 15:39:23 +00:00
|
|
|
/// license = "MIT OR Apache-2.0"
|
2019-03-05 16:50:33 +00:00
|
|
|
/// keywords = ["clippy", "lint", "plugin"]
|
|
|
|
/// categories = ["development-tools", "development-tools::cargo-plugins"]
|
|
|
|
/// ```
|
2018-11-13 13:43:30 +00:00
|
|
|
pub CARGO_COMMON_METADATA,
|
|
|
|
cargo,
|
|
|
|
"common metadata is defined in `Cargo.toml`"
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
|
2018-11-13 13:43:30 +00:00
|
|
|
let message = format!("package `{}` is missing `{}` metadata", package.name, field);
|
2020-06-09 14:36:01 +00:00
|
|
|
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
|
2018-11-13 13:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty_str(value: &Option<String>) -> bool {
|
2019-09-04 14:19:59 +00:00
|
|
|
value.as_ref().map_or(true, String::is_empty)
|
2018-11-13 13:43:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 08:00:02 +00:00
|
|
|
fn is_empty_path(value: &Option<PathBuf>) -> bool {
|
|
|
|
value.as_ref().and_then(|x| x.to_str()).map_or(true, str::is_empty)
|
|
|
|
}
|
|
|
|
|
2018-11-13 13:43:30 +00:00
|
|
|
fn is_empty_vec(value: &[String]) -> bool {
|
|
|
|
// This works because empty iterators return true
|
2019-12-23 20:06:52 +00:00
|
|
|
value.iter().all(String::is_empty)
|
2018-11-13 13:43:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]);
|
2018-11-13 13:43:30 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl LateLintPass<'_> for CargoCommonMetadata {
|
|
|
|
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
2020-04-21 20:53:18 +00:00
|
|
|
if !run_lints(cx, &[CARGO_COMMON_METADATA], CRATE_HIR_ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-09 14:36:01 +00:00
|
|
|
let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
|
2018-11-13 13:43:30 +00:00
|
|
|
|
|
|
|
for package in metadata.packages {
|
|
|
|
if is_empty_vec(&package.authors) {
|
|
|
|
missing_warning(cx, &package, "package.authors");
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_empty_str(&package.description) {
|
|
|
|
missing_warning(cx, &package, "package.description");
|
|
|
|
}
|
|
|
|
|
2019-09-07 08:00:02 +00:00
|
|
|
if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
|
|
|
|
missing_warning(cx, &package, "either package.license or package.license_file");
|
2018-11-13 13:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if is_empty_str(&package.repository) {
|
|
|
|
missing_warning(cx, &package, "package.repository");
|
|
|
|
}
|
|
|
|
|
2019-10-23 15:30:24 +00:00
|
|
|
if is_empty_path(&package.readme) {
|
2018-11-13 13:43:30 +00:00
|
|
|
missing_warning(cx, &package, "package.readme");
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_empty_vec(&package.keywords) {
|
|
|
|
missing_warning(cx, &package, "package.keywords");
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_empty_vec(&package.categories) {
|
|
|
|
missing_warning(cx, &package, "package.categories");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|