@@ -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 ! {
@@ -42,15 +45,34 @@ declare_clippy_lint! {
42
45
pub ( crate ) struct IncludeFileOutsideProject {
43
46
cargo_manifest_dir : Option < PathBuf > ,
44
47
warned_spans : FxHashSet < PathBuf > ,
48
+ can_check_crate : bool ,
45
49
}
46
50
47
51
impl_lint_pass ! ( IncludeFileOutsideProject => [ INCLUDE_FILE_OUTSIDE_PROJECT ] ) ;
48
52
49
53
impl IncludeFileOutsideProject {
50
- pub ( crate ) fn new ( ) -> Self {
54
+ pub ( crate ) fn new ( conf : & ' static Conf ) -> Self {
55
+ let mut can_check_crate = true ;
56
+ if !conf. cargo_ignore_publish {
57
+ match MetadataCommand :: new ( ) . no_deps ( ) . exec ( ) {
58
+ Ok ( metadata) => {
59
+ for package in & metadata. packages {
60
+ // only run the lint if publish is `None` (`publish = true` or skipped entirely)
61
+ // or if the vector isn't empty (`publish = ["something"]`)
62
+ if package. publish . as_ref ( ) . filter ( |publish| publish. is_empty ( ) ) . is_some ( ) {
63
+ can_check_crate = false ;
64
+ break ;
65
+ }
66
+ }
67
+ } ,
68
+ Err ( _) => can_check_crate = false ,
69
+ }
70
+ }
71
+
51
72
Self {
52
- cargo_manifest_dir : std:: env:: var ( "CARGO_MANIFEST_DIR" ) . ok ( ) . map ( |dir| PathBuf :: from ( dir ) ) ,
73
+ cargo_manifest_dir : std:: env:: var ( "CARGO_MANIFEST_DIR" ) . ok ( ) . map ( PathBuf :: from) ,
53
74
warned_spans : FxHashSet :: default ( ) ,
75
+ can_check_crate,
54
76
}
55
77
}
56
78
@@ -72,12 +94,12 @@ impl IncludeFileOutsideProject {
72
94
}
73
95
}
74
96
75
- fn is_part_of_project_dir ( & self , file_path : & PathBuf ) -> bool {
97
+ fn is_part_of_project_dir ( & self , file_path : & Path ) -> bool {
76
98
if let Some ( ref cargo_manifest_dir) = self . cargo_manifest_dir {
77
99
// Check if both paths start with the same thing.
78
100
let mut file_iter = file_path. iter ( ) ;
79
101
80
- for cargo_item in cargo_manifest_dir. iter ( ) {
102
+ for cargo_item in cargo_manifest_dir {
81
103
match file_iter. next ( ) {
82
104
Some ( file_path) if file_path == cargo_item => { } ,
83
105
_ => {
@@ -141,6 +163,9 @@ impl IncludeFileOutsideProject {
141
163
142
164
impl LateLintPass < ' _ > for IncludeFileOutsideProject {
143
165
fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & ' _ Expr < ' _ > ) {
166
+ if !self . can_check_crate {
167
+ return ;
168
+ }
144
169
if !expr. span . from_expansion ( ) {
145
170
self . check_hir_id ( cx, expr. span , expr. hir_id ) ;
146
171
} else if let ExprKind :: Lit ( lit) = & expr. kind
@@ -156,12 +181,15 @@ impl LateLintPass<'_> for IncludeFileOutsideProject {
156
181
fn check_item ( & mut self , cx : & LateContext < ' _ > , item : & ' _ Item < ' _ > ) {
157
182
// Interestingly enough, `include!` content is not considered expanded. Which allows us
158
183
// to easily filter out items we're not interested into.
159
- if !item. span . from_expansion ( ) {
184
+ if self . can_check_crate && !item. span . from_expansion ( ) {
160
185
self . check_hir_id ( cx, item. span , item. hir_id ( ) ) ;
161
186
}
162
187
}
163
188
164
189
fn check_attributes ( & mut self , cx : & LateContext < ' _ > , attrs : & [ Attribute ] ) {
190
+ if !self . can_check_crate {
191
+ return ;
192
+ }
165
193
for attr in attrs {
166
194
if let Some ( attr) = attr. meta ( ) {
167
195
self . check_attribute ( cx, & attr) ;
0 commit comments