2018-11-13 07:43:30 -06:00
|
|
|
//! lint on missing cargo common metadata
|
|
|
|
|
2021-07-15 03:44:10 -05:00
|
|
|
use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
|
2021-09-12 04:58:27 -05:00
|
|
|
use rustc_hir::hir_id::CRATE_HIR_ID;
|
2020-04-21 15:53:18 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-02-11 08:04:38 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2020-01-04 04:00:00 -06:00
|
|
|
use rustc_span::source_map::DUMMY_SP;
|
2018-11-13 07:43:30 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks to see if all common metadata is defined in
|
2019-03-05 10:50:33 -06:00
|
|
|
/// `Cargo.toml`. See: https://rust-lang-nursery.github.io/api-guidelines/documentation.html#cargotoml-includes-all-common-metadata-c-metadata
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It will be more difficult for users to discover the
|
2019-03-05 10:50:33 -06:00
|
|
|
/// purpose of the crate, and key information related to it.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```toml
|
2021-04-08 10:50:13 -05:00
|
|
|
/// # This `Cargo.toml` is missing a description field:
|
2019-03-05 10:50:33 -06:00
|
|
|
/// [package]
|
|
|
|
/// name = "clippy"
|
|
|
|
/// version = "0.0.212"
|
2020-11-23 06:51:04 -06:00
|
|
|
/// 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"]
|
|
|
|
/// ```
|
|
|
|
///
|
2021-04-08 10:50:13 -05:00
|
|
|
/// Should include a description field like:
|
2020-11-23 06:51:04 -06:00
|
|
|
///
|
|
|
|
/// ```toml
|
|
|
|
/// # This `Cargo.toml` includes all common metadata
|
|
|
|
/// [package]
|
|
|
|
/// name = "clippy"
|
|
|
|
/// version = "0.0.212"
|
2019-03-05 10:50:33 -06: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 10:39:23 -05:00
|
|
|
/// license = "MIT OR Apache-2.0"
|
2019-03-05 10:50:33 -06:00
|
|
|
/// keywords = ["clippy", "lint", "plugin"]
|
|
|
|
/// categories = ["development-tools", "development-tools::cargo-plugins"]
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "1.32.0"]
|
2018-11-13 07:43:30 -06:00
|
|
|
pub CARGO_COMMON_METADATA,
|
|
|
|
cargo,
|
|
|
|
"common metadata is defined in `Cargo.toml`"
|
|
|
|
}
|
|
|
|
|
2021-02-11 08:04:38 -06:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct CargoCommonMetadata {
|
|
|
|
ignore_publish: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CargoCommonMetadata {
|
|
|
|
pub fn new(ignore_publish: bool) -> Self {
|
|
|
|
Self { ignore_publish }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(CargoCommonMetadata => [
|
|
|
|
CARGO_COMMON_METADATA
|
|
|
|
]);
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
|
2018-11-13 07:43:30 -06:00
|
|
|
let message = format!("package `{}` is missing `{}` metadata", package.name, field);
|
2020-06-09 09:36:01 -05:00
|
|
|
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
|
2018-11-13 07:43:30 -06:00
|
|
|
}
|
|
|
|
|
2021-09-01 17:31:42 -05:00
|
|
|
fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: &Option<T>) -> bool {
|
|
|
|
value.as_ref().map_or(true, |s| s.as_ref().is_empty())
|
2019-09-07 03:00:02 -05:00
|
|
|
}
|
|
|
|
|
2018-11-13 07:43:30 -06:00
|
|
|
fn is_empty_vec(value: &[String]) -> bool {
|
|
|
|
// This works because empty iterators return true
|
2019-12-23 14:06:52 -06:00
|
|
|
value.iter().all(String::is_empty)
|
2018-11-13 07:43:30 -06:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl LateLintPass<'_> for CargoCommonMetadata {
|
2021-09-12 04:58:27 -05:00
|
|
|
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
2021-07-15 03:44:10 -05:00
|
|
|
if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) {
|
2020-04-21 15:53:18 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:36:01 -05:00
|
|
|
let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
|
2018-11-13 07:43:30 -06:00
|
|
|
|
|
|
|
for package in metadata.packages {
|
2021-02-11 08:04:38 -06:00
|
|
|
// only run the lint if publish is `None` (`publish = true` or skipped entirely)
|
|
|
|
// or if the vector isn't empty (`publish = ["something"]`)
|
|
|
|
if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
|
|
|
|
if is_empty_str(&package.description) {
|
|
|
|
missing_warning(cx, &package, "package.description");
|
|
|
|
}
|
|
|
|
|
2021-09-01 17:31:42 -05:00
|
|
|
if is_empty_str(&package.license) && is_empty_str(&package.license_file) {
|
2021-02-11 08:04:38 -06:00
|
|
|
missing_warning(cx, &package, "either package.license or package.license_file");
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_empty_str(&package.repository) {
|
|
|
|
missing_warning(cx, &package, "package.repository");
|
|
|
|
}
|
|
|
|
|
2021-09-01 17:31:42 -05:00
|
|
|
if is_empty_str(&package.readme) {
|
2021-02-11 08:04:38 -06: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");
|
|
|
|
}
|
2018-11-13 07:43:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|