@@ -5,7 +5,7 @@ use std::path::PathBuf;
55use crate :: utils:: { run_lints, span_lint} ;
66use rustc_hir:: { hir_id:: CRATE_HIR_ID , Crate } ;
77use rustc_lint:: { LateContext , LateLintPass } ;
8- use rustc_session:: { declare_lint_pass , declare_tool_lint } ;
8+ use rustc_session:: { declare_tool_lint , impl_lint_pass } ;
99use rustc_span:: source_map:: DUMMY_SP ;
1010
1111declare_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+
5469fn 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-
7487impl 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 }
0 commit comments