@@ -5,9 +5,12 @@ use rustc_lint::{LateContext, LateLintPass};
5
5
use rustc_session:: impl_lint_pass;
6
6
use rustc_span:: { FileName , Span , sym} ;
7
7
8
+ use clippy_config:: Conf ;
8
9
use clippy_utils:: diagnostics:: span_lint_and_then;
9
10
use clippy_utils:: macros:: root_macro_call_first_node;
10
11
12
+ use cargo_metadata:: MetadataCommand ;
13
+
11
14
use std:: path:: { Path , PathBuf } ;
12
15
13
16
declare_clippy_lint ! {
@@ -36,15 +39,34 @@ declare_clippy_lint! {
36
39
pub ( crate ) struct IncludeFileOutsideProject {
37
40
cargo_manifest_dir : Option < PathBuf > ,
38
41
warned_spans : FxHashSet < PathBuf > ,
42
+ can_check_crate : bool ,
39
43
}
40
44
41
45
impl_lint_pass ! ( IncludeFileOutsideProject => [ INCLUDE_FILE_OUTSIDE_PROJECT ] ) ;
42
46
43
47
impl IncludeFileOutsideProject {
44
- pub ( crate ) fn new ( ) -> Self {
48
+ pub ( crate ) fn new ( conf : & ' static Conf ) -> Self {
49
+ let mut can_check_crate = true ;
50
+ if !conf. cargo_ignore_publish {
51
+ match MetadataCommand :: new ( ) . no_deps ( ) . exec ( ) {
52
+ Ok ( metadata) => {
53
+ for package in & metadata. packages {
54
+ // only run the lint if publish is `None` (`publish = true` or skipped entirely)
55
+ // or if the vector isn't empty (`publish = ["something"]`)
56
+ if !package. publish . as_ref ( ) . filter ( |publish| publish. is_empty ( ) ) . is_none ( ) {
57
+ can_check_crate = false ;
58
+ break ;
59
+ }
60
+ }
61
+ } ,
62
+ Err ( _) => can_check_crate = false ,
63
+ }
64
+ }
65
+
45
66
Self {
46
67
cargo_manifest_dir : std:: env:: var ( "CARGO_MANIFEST_DIR" ) . ok ( ) . map ( |dir| PathBuf :: from ( dir) ) ,
47
68
warned_spans : FxHashSet :: default ( ) ,
69
+ can_check_crate,
48
70
}
49
71
}
50
72
@@ -135,6 +157,9 @@ impl IncludeFileOutsideProject {
135
157
136
158
impl LateLintPass < ' _ > for IncludeFileOutsideProject {
137
159
fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & ' _ Expr < ' _ > ) {
160
+ if !self . can_check_crate {
161
+ return ;
162
+ }
138
163
if !expr. span . from_expansion ( ) {
139
164
self . check_hir_id ( cx, expr. span , expr. hir_id ) ;
140
165
} else if let ExprKind :: Lit ( lit) = & expr. kind
@@ -150,12 +175,15 @@ impl LateLintPass<'_> for IncludeFileOutsideProject {
150
175
fn check_item ( & mut self , cx : & LateContext < ' _ > , item : & ' _ Item < ' _ > ) {
151
176
// Interestingly enough, `include!` content is not considered expanded. Which allows us
152
177
// to easily filter out items we're not interested into.
153
- if !item. span . from_expansion ( ) {
178
+ if self . can_check_crate && !item. span . from_expansion ( ) {
154
179
self . check_hir_id ( cx, item. span , item. hir_id ( ) ) ;
155
180
}
156
181
}
157
182
158
183
fn check_attributes ( & mut self , cx : & LateContext < ' _ > , attrs : & [ Attribute ] ) {
184
+ if !self . can_check_crate {
185
+ return ;
186
+ }
159
187
for attr in attrs {
160
188
if let Some ( attr) = attr. meta ( ) {
161
189
self . check_attribute ( cx, & attr) ;
0 commit comments