Skip to content

Commit beb49ba

Browse files
committed
Auto merge of #6650 - daxpedda:cargo-common-metadata-publish, r=flip1995
Fix cargo_common_metadata warning on `publish = false`. I believe `cargo_common_metadata` shouldn't trigger when `publish = false`, not sure if everybody agrees. Made some tests to handle all edge-cases. Fixes #6649. changelog: [`cargo_common_metadata`](https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata): No longer lints if [`publish = false`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-publish-field) is defined in the manifest
2 parents 3784cdf + fd8b5fa commit beb49ba

File tree

17 files changed

+132
-31
lines changed

17 files changed

+132
-31
lines changed

Diff for: clippy_lints/src/cargo_common_metadata.rs

+46-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::PathBuf;
55
use crate::utils::{run_lints, span_lint};
66
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
77
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::source_map::DUMMY_SP;
1010

1111
declare_clippy_lint! {
@@ -51,6 +51,21 @@ declare_clippy_lint! {
5151
"common metadata is defined in `Cargo.toml`"
5252
}
5353

54+
#[derive(Copy, Clone, Debug)]
55+
pub struct CargoCommonMetadata {
56+
ignore_publish: bool,
57+
}
58+
59+
impl CargoCommonMetadata {
60+
pub fn new(ignore_publish: bool) -> Self {
61+
Self { ignore_publish }
62+
}
63+
}
64+
65+
impl_lint_pass!(CargoCommonMetadata => [
66+
CARGO_COMMON_METADATA
67+
]);
68+
5469
fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, field: &str) {
5570
let message = format!("package `{}` is missing `{}` metadata", package.name, field);
5671
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
@@ -69,8 +84,6 @@ fn is_empty_vec(value: &[String]) -> bool {
6984
value.iter().all(String::is_empty)
7085
}
7186

72-
declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]);
73-
7487
impl LateLintPass<'_> for CargoCommonMetadata {
7588
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
7689
if !run_lints(cx, &[CARGO_COMMON_METADATA], CRATE_HIR_ID) {
@@ -80,32 +93,36 @@ impl LateLintPass<'_> for CargoCommonMetadata {
8093
let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
8194

8295
for package in metadata.packages {
83-
if is_empty_vec(&package.authors) {
84-
missing_warning(cx, &package, "package.authors");
85-
}
86-
87-
if is_empty_str(&package.description) {
88-
missing_warning(cx, &package, "package.description");
89-
}
90-
91-
if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
92-
missing_warning(cx, &package, "either package.license or package.license_file");
93-
}
94-
95-
if is_empty_str(&package.repository) {
96-
missing_warning(cx, &package, "package.repository");
97-
}
98-
99-
if is_empty_path(&package.readme) {
100-
missing_warning(cx, &package, "package.readme");
101-
}
102-
103-
if is_empty_vec(&package.keywords) {
104-
missing_warning(cx, &package, "package.keywords");
105-
}
106-
107-
if is_empty_vec(&package.categories) {
108-
missing_warning(cx, &package, "package.categories");
96+
// only run the lint if publish is `None` (`publish = true` or skipped entirely)
97+
// or if the vector isn't empty (`publish = ["something"]`)
98+
if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || self.ignore_publish {
99+
if is_empty_vec(&package.authors) {
100+
missing_warning(cx, &package, "package.authors");
101+
}
102+
103+
if is_empty_str(&package.description) {
104+
missing_warning(cx, &package, "package.description");
105+
}
106+
107+
if is_empty_str(&package.license) && is_empty_path(&package.license_file) {
108+
missing_warning(cx, &package, "either package.license or package.license_file");
109+
}
110+
111+
if is_empty_str(&package.repository) {
112+
missing_warning(cx, &package, "package.repository");
113+
}
114+
115+
if is_empty_path(&package.readme) {
116+
missing_warning(cx, &package, "package.readme");
117+
}
118+
119+
if is_empty_vec(&package.keywords) {
120+
missing_warning(cx, &package, "package.keywords");
121+
}
122+
123+
if is_empty_vec(&package.categories) {
124+
missing_warning(cx, &package, "package.categories");
125+
}
109126
}
110127
}
111128
}

Diff for: clippy_lints/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11841184
store.register_early_pass(|| box redundant_else::RedundantElse);
11851185
store.register_late_pass(|| box create_dir::CreateDir);
11861186
store.register_early_pass(|| box needless_arbitrary_self_type::NeedlessArbitrarySelfType);
1187-
store.register_late_pass(|| box cargo_common_metadata::CargoCommonMetadata);
1187+
let cargo_ignore_publish = conf.cargo_ignore_publish;
1188+
store.register_late_pass(move || box cargo_common_metadata::CargoCommonMetadata::new(cargo_ignore_publish));
11881189
store.register_late_pass(|| box multiple_crate_versions::MultipleCrateVersions);
11891190
store.register_late_pass(|| box wildcard_dependencies::WildcardDependencies);
11901191
let literal_representation_lint_fraction_readability = conf.unreadable_literal_lint_fractions;

Diff for: clippy_lints/src/utils/conf.rs

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ define_Conf! {
173173
(disallowed_methods, "disallowed_methods": Vec<String>, Vec::<String>::new()),
174174
/// Lint: UNREADABLE_LITERAL. Should the fraction of a decimal be linted to include separators.
175175
(unreadable_literal_lint_fractions, "unreadable_literal_lint_fractions": bool, true),
176+
/// Lint: _CARGO_COMMON_METADATA. For internal testing only, ignores the current `publish` settings in the Cargo manifest.
177+
(cargo_ignore_publish, "cargo_ignore_publish": bool, false),
176178
}
177179

178180
impl Default for Conf {

Diff for: tests/compile-test.rs

+3
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
214214
Some("main.rs") => {},
215215
_ => continue,
216216
}
217+
set_var("CLIPPY_CONF_DIR", case.path());
217218
let paths = compiletest::common::TestPaths {
218219
file: file_path,
219220
base: config.src_base.clone(),
@@ -241,9 +242,11 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
241242
let tests = compiletest::make_tests(&config);
242243

243244
let current_dir = env::current_dir().unwrap();
245+
let conf_dir = var("CLIPPY_CONF_DIR").unwrap_or_default();
244246
let filter = env::var("TESTNAME").ok();
245247
let res = run_tests(&config, &filter, tests);
246248
env::set_current_dir(current_dir).unwrap();
249+
set_var("CLIPPY_CONF_DIR", conf_dir);
247250

248251
match res {
249252
Ok(true) => {},
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cargo-ignore-publish = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "cargo_common_metadata"
3+
version = "0.1.0"
4+
publish = ["some-registry-name"]
5+
6+
[workspace]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: --crate-name=cargo_common_metadata
2+
#![warn(clippy::cargo_common_metadata)]
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: package `cargo_common_metadata` is missing `package.authors` metadata
2+
|
3+
= note: `-D clippy::cargo-common-metadata` implied by `-D warnings`
4+
5+
error: package `cargo_common_metadata` is missing `package.description` metadata
6+
7+
error: package `cargo_common_metadata` is missing `either package.license or package.license_file` metadata
8+
9+
error: package `cargo_common_metadata` is missing `package.repository` metadata
10+
11+
error: package `cargo_common_metadata` is missing `package.readme` metadata
12+
13+
error: package `cargo_common_metadata` is missing `package.keywords` metadata
14+
15+
error: package `cargo_common_metadata` is missing `package.categories` metadata
16+
17+
error: aborting due to 7 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "cargo_common_metadata"
3+
version = "0.1.0"
4+
publish = true
5+
6+
[workspace]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: --crate-name=cargo_common_metadata
2+
#![warn(clippy::cargo_common_metadata)]
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: package `cargo_common_metadata` is missing `package.authors` metadata
2+
|
3+
= note: `-D clippy::cargo-common-metadata` implied by `-D warnings`
4+
5+
error: package `cargo_common_metadata` is missing `package.description` metadata
6+
7+
error: package `cargo_common_metadata` is missing `either package.license or package.license_file` metadata
8+
9+
error: package `cargo_common_metadata` is missing `package.repository` metadata
10+
11+
error: package `cargo_common_metadata` is missing `package.readme` metadata
12+
13+
error: package `cargo_common_metadata` is missing `package.keywords` metadata
14+
15+
error: package `cargo_common_metadata` is missing `package.categories` metadata
16+
17+
error: aborting due to 7 previous errors
18+
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cargo-ignore-publish = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "cargo_common_metadata"
3+
version = "0.1.0"
4+
publish = []
5+
6+
[workspace]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: --crate-name=cargo_common_metadata
2+
#![warn(clippy::cargo_common_metadata)]
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "cargo_common_metadata"
3+
version = "0.1.0"
4+
publish = false
5+
6+
[workspace]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: --crate-name=cargo_common_metadata
2+
#![warn(clippy::cargo_common_metadata)]
3+
4+
fn main() {}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `unreadable-literal-lint-fractions`, `third-party` at line 5 column 1
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `unreadable-literal-lint-fractions`, `cargo-ignore-publish`, `third-party` at line 5 column 1
22

33
error: aborting due to previous error
44

0 commit comments

Comments
 (0)