Skip to content

Commit

Permalink
Implement _cargo_ignore_publish.
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Feb 6, 2021
1 parent d4bc7d2 commit f0d3fd7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
21 changes: 17 additions & 4 deletions clippy_lints/src/cargo_common_metadata.rs
Expand Up @@ -5,7 +5,7 @@ use std::path::PathBuf;
use crate::utils::{run_lints, span_lint};
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::DUMMY_SP;

declare_clippy_lint! {
Expand Down Expand Up @@ -51,6 +51,21 @@ declare_clippy_lint! {
"common metadata is defined in `Cargo.toml`"
}

#[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
]);

fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
let message = format!("package `{}` is missing `{}` metadata", package.name, field);
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
Expand All @@ -69,8 +84,6 @@ fn is_empty_vec(value: &[String]) -> bool {
value.iter().all(String::is_empty)
}

declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]);

impl LateLintPass<'_> for CargoCommonMetadata {
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
if !run_lints(cx, &[CARGO_COMMON_METADATA], CRATE_HIR_ID) {
Expand All @@ -80,7 +93,7 @@ impl LateLintPass<'_> for CargoCommonMetadata {
let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);

for package in metadata.packages {
if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() {
if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
if is_empty_vec(&package.authors) {
missing_warning(cx, &package, "package.authors");
}
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/lib.rs
Expand Up @@ -1180,7 +1180,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_early_pass(|| box redundant_else::RedundantElse);
store.register_late_pass(|| box create_dir::CreateDir);
store.register_early_pass(|| box needless_arbitrary_self_type::NeedlessArbitrarySelfType);
store.register_late_pass(|| box cargo_common_metadata::CargoCommonMetadata);
let cargo_ignore_publish = conf._cargo_ignore_publish;
store.register_late_pass(move || box cargo_common_metadata::CargoCommonMetadata::new(cargo_ignore_publish));
store.register_late_pass(|| box multiple_crate_versions::MultipleCrateVersions);
store.register_late_pass(|| box wildcard_dependencies::WildcardDependencies);
let literal_representation_lint_fraction_readability = conf.unreadable_literal_lint_fractions;
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/utils/conf.rs
Expand Up @@ -173,6 +173,8 @@ define_Conf! {
(disallowed_methods, "disallowed_methods": Vec<String>, Vec::<String>::new()),
/// Lint: UNREADABLE_LITERAL. Should the fraction of a decimal be linted to include separators.
(unreadable_literal_lint_fractions, "unreadable_literal_lint_fractions": bool, true),
/// Lint: CARGO_COMMON_METADATA. For internal testing only, ignores the current `publish` settings in the Cargo manifest.
(_cargo_ignore_publish, "_cargo_ignore_publish": bool, false),
}

impl Default for Conf {
Expand Down
1 change: 1 addition & 0 deletions tests/ui-cargo/cargo_common_metadata/pass/Cargo.toml
@@ -1,6 +1,7 @@
[package]
name = "cargo_common_metadata"
version = "0.1.0"
publish = false
authors = ["Random person from the Internet <someone@someplace.org>"]
description = "A test package for the cargo_common_metadata lint"
repository = "https://github.com/someone/cargo_common_metadata"
Expand Down
1 change: 1 addition & 0 deletions tests/ui-cargo/cargo_common_metadata/pass/clippy.toml
@@ -0,0 +1 @@
_cargo_ignore_publish = true

0 comments on commit f0d3fd7

Please sign in to comment.